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

So I’ve been trying to solve this step for the past 2 hours and can’t quite understand what I am supposed to do besides what I’ve written, if anyone could point me in the right direction as to why isn’t my code working since I’ve tried it on jsfiddle and it does comply with what the prompt asks me. This is my code so far:

``````const getMode = (array) => {
const counts = {};
array.forEach((el) => {
if (counts.hasOwnProperty(el)) { //Also tried in arrow form.
counts[el]++; // Setting the count property.
} else {
counts[el] = 1; // Fallback.
}
});
}
``````

I would say that the instructions for this exercise have been somewhat vague and the hints feels lacking in guidance. Other than that I’m very happy with this new beta course. Thank you in advance guys

Were you able to find a solution? I’m stuck on this same step

Nope.
I’m resisting the urge to ask ChatGPT cause I want to solve it by myself .
When I manage to solve it I’ll let you know .

Here’s an article you may find helpful.

Hmm thank you, but still can’t solve it.
For future reference: I modified my original code to also check if counts[el] is a number since .hasOwnProperty returns true even if the property is undefined. So my final if looks like this:

``````if(counts.hasOwnProperty(el) && !isNaN(counts[el])) {
``````

At this point I’m guessing it’s a bug since it does comply with the prompt. I guess I’ll wait 'till they fix it. Nonetheless if I’m doing something wrong please tell me

Since you are using the forEach method, wouldn’t the array item already exist?

However, your codes look good.

So in theory this code should work?

``````array.forEach((el) => {
if(counts[el]) {
counts[el]++;
} else {
counts[el] = 1;
}
``````

This was my first code, tested both and they both work,
Just to clarify things, is it me who is not understanding something, or is this step broken?

I believe this step will be fixed to allow any valid solution. The PR isn’t done and it will need to be pushed to production as well.

For now, the solution is fixed and tested using a regex so you have to solve it using that one specific solution. You can jump to the next step to see it as it isn’t super likely that you will land on the exact code the solution requires.

2 Likes

I am completely lost here too. What does it want us to do? Is this a bug?

At my current level of knowledge I would never have been able to solve it thanks for the reply

I would suggest you skip ahead (use the browser address bar and go to step 28) then just look at the solution code and go back and write it.

I would consider it a bug, but really it is just too strict a test.

2 Likes

use || as a fallback mechanism instead of if statements.

Mod edit: solution code removed

I had the same issue, although my solution was ok, it was:

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

But as @ritesh.poudel.34 already posted, that’s the official solution and the only one the test will be happy on.

Please do not post the solution code.

If anyone wants the solution they can skip forward. But it should not be posted on the forum.

This step is being worked on and should get updated at some point.

1 Like