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

The instruction for this step says:

Use the .forEach() method to loop through the array . In the callback, use the el argument to access the counts object and increment the count for each number.

My code so far

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

}

I’m trying to check, for each element, if the key is already defined in the object and to increment if it is; and set the key to be 1 when it isn’t already present in the object. But ultimately the test is failing. Please, what am I doing wrong. Am I just completely misinterpreting the instruction?

Challenge Information:

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

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

ChatGPT gave me an answer that I wouldn’t even have thought of, and it passed. It’s always my last resort to use language models (LLMs), and I always ask Bard and ChatGPT to break down the answers they give me.

I see that you’re using an if-else statement in ternary form, but what I saw on YouTube was just a regular if-else, which should be correct too. But the test accepted something that’s too incomprehensible for me because it only uses the OR logical operator, unlike what ChatGPT answered.

I don’t know if I’m missing something, but it really frustrates me because I wasn’t prepared for this type of logic. Maybe it’s because the course was in beta mode, and that’s why it’s like that. I even tell Bard and ChatGPT to break down the correct code for me, but I still don’t understand.

It took me ages to figure it out, used if statements, ternary operator, rewrote it multiple way, no luck. But I decided to ask chatgpt and this answer worked.

[spoiler]

You would think that this solution should work, but it doesn’t.

dear freeCodeCamp staff, I totally get that it’s very hard to make a question that can have many ways to answer. I noticed throughout this specific course that I hit dead ends more often and have to resort to chatGPT. So, please make hints have a more explicit solution. This will greatly help when you submitted the “wrong” answer so that you don’t have to run in circles wondering why it doesn’t work.

Thank you for providing the code. If we are counting NaNs as 0, why doesn’t that skew the Mode value? For example, if someone inputted [1, 1, h, h, h] the mode would be 0 instead of 1? Shouldn’t those values be filtered out?

@aAsm321

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.

1 Like

Hi all, I was still stuck on this. I eventually had to ask chat gpt for a solution which worked. I tried to reimplement it with another logic to see if i understood it, it failed again. Apparently I have not understood what is going on. I am not posting the solution here to promote learning, but can someone please break it down for me? thanks. It must be so frustrating for others too since the question does not provide so much clarity other than ‘Swim and don’t drown’ :sweat_smile:

1 Like

hey i have the same question and cant seem to get an answer i thought the above solution would work as well
what did you use ?

1 Like

I am writing to up this topic

Had the same issue but it’s been resolved: Learn Advanced Array Methods by Building a Statistics Calculator - Step 27

1 Like

I’m still thankful for the existence of FreeCodeCamp. It has helped me to understand MDN. Developers are correct in saying that you don’t want to be spoon-fed. To me, fCC already feels like being spoon-fed. So whenever I ask questions to LLMs, I always request them to break it down and explain why it is written in such a way to the point it will say it’s the design of the interpreter. I use LLMs cause I just don’t want to be burden to the staff of fCC.