I'm new to the world of javascript and when doing a reduce for an array, I want to try to count the number of times the age is equal in an array of objects whose properties are the ages.
However when I do a ternary conditional I receive that the output is undefined.
but when doing a normal if with an else if you can do the count:
const numbers = [ {age: 4, age2: 4}, {age: 5, age2: 6}, {age: 88, age2: 99}, {age:14, age2:14}, {age: 2, age2: 2} ];
const getCount = ( objects ) => objects.reduce((acc, el) => {
if ( el.x === el.y ) {
return acc += 1;
}
else {
return acc;
}
//el.x === el.y ? acc += 1 : acc;
}, 0);
const b = getCount(numbers)
console.log(b)
you can comment on the if, and try it with the ternary and see what I mean
Please I do not understand what happens, what is the error