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

Tell us what’s happening:

I tried replacing the if statement with a ternary operator but can’t make it work. What’s wrong with my code?

Your code so far

// User Editable Region

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

// User Editable Region


const calculate = () => {
  const value = document.querySelector("#numbers").value;
  const array = value.split(/,\s*/g);
  const numbers = array.map(el => Number(el)).filter(el => !isNaN(el));
  
  const mean = getMean(numbers);
  const median = getMedian(numbers);
  console.log(getMode(numbers));

  document.querySelector("#mean").textContent = mean;
  document.querySelector("#median").textContent = median;
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Safari/605.1.15

Challenge Information:

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

Hi there!

The link is not working, post the link again

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

I did exactly same. I don’t know ever

hey @MichaelO

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.

hi @chinkhuslenerdenebil , your issue is that the ternary operator needs to go to the right of the assignment operator. You should not have an assignment inside the ternary

Hello I review your code there is something missing which i noticed is assign operator which is = .
after your (el => counts[el] put assign operator = .
after this your code look like this code removed by moderator
also you do not write again counts[el] again in your code to do counts[el] = 1 because this show your editor that you again assign number to your ternary opeartor and it gives you error.
ok Bye Happy Coding !

hi @satyam10

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.