Learn advanced array methods by building a statistics calculator step 35

i am having trouble clearing this step. can a senior please explain in simple words what i am doing wrong

const getMode = (array) => {

const counts = {};

array.forEach((el) => counts[el] = counts[el] ? counts[el] + 1 : 1);

return counts;

}

Mode is the value that appears most often not the whole object of counts.

So instead of returning object, you need to:

  1. Find which key has the highest count.

  2. Return that key (or number).

noted, but that part is not the problem its the for each which is saying i need a tarenary operator in my function which i think is already there but still it wont accept it

array.forEach((el) => counts[el] = counts[el] ? counts[el] + 1 : 1);

this

Your ternary is actually fine, the problem usually comes from extra braces or missing semicolons. Either use:

array.forEach(el => counts[el] = counts[el] ? counts[el] + 1 : 1);

or.

array.forEach(el => {
  counts[el] = counts[el] ? counts[el] + 1 : 1;
});
1 Like

Please post your full code and a link to the challenge you are working on so we can test.

i got it now thank you :grin: