Learn Advanced Array Methods by Building a Statistics Calculator - Step 35

Tell us what’s happening:

I don’t why my code isn’t passing. I am pretty sure that I am using ternary operator and that the code is working.

Error:
Your function should use a ternary operator.

Your code so far

// User Editable Region

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

// User Editable Region

Challenge Information:

Learn Advanced Array Methods by Building a Statistics Calculator - Step 35

Use the format in the example code. You should have a variable that you are assigning a value to. Don’t use an assignment within the ternary like this : counts[el]=1)

Example Code

assignment = condition ? exprIfTrue : exprIfFalse

Your code is starting with the condition.

Do I do something like this?

const getMode = (array) => {
  const counts = {};
  array.forEach(el => {

    counts[el] = counts[el] ? +1 : 1

    
  });
  return counts;
}

the ternary is evaluating to 1 in both cases

in one of the two cases you want to increase the current value of counts[el]

This would evaluate to one of these:

counts[el] = +1
counts[el] = 1

I managed to solve it thx!

1 Like

how ? I have the exact same as your original code and cant figure out what I have done wrong

hi @sking7
please create your own topic

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge. This button only appears if you have tried to submit an answer at least three times.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.