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

Howdy,

This code seems to output what the challenge wants me to output, but it won’t pass. Can someone advise? (I have added the return and console.log statements to check that it does what the challenge tells me to do)

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

console.log(getMode([1,2,3,4,5,5,5,5])) //==> { '1': 1, '2': 1, '3': 1, '4': 1, '5': 4 }

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36

Challenge Information:

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

the challenge wants that you check the value or put 0 and then do the sum

The instructions read:
" Use the .forEach() method to loop through the array . In the callback, use the el parameter to access the counts object and increment the count for each number."

The following code accesses the counts object and increments the count of each number in the array. Where are the instructions to put 0 or do the sum coming from? If I am to sum, then what should I be summing? If I am iterating through an array of values, how will I ever have a case where I would need to assign the value of 0 to a property of the counts object?

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

console.log(getMode([1,2,2,3,4,5,5,5]));  ==>

//{ '1': 1, '2': 2, '3': 1, '4': 1, '5': 3 }

this is correct, but you need to use the code with the OR logical operator and the fallback to 0 for the tests to be happy

Thank you for your quick responses. I do appreciate them.

However, if I provide 0 for a fall back, the count is not accurate (numbers occurring only once will have a property value of 0)

counts[el] = (counts[el] + 1) || 0;
console.log(getMode([1,2,2,2,3]))
{ '1': 0, '2': 2, '3': 0 }

almost, the fallback would be only for the value of counts[el], so you add 1 to counts[el] OR to 0

Interesting, so

counts[el] = (counts[el] || 0 ) + 1

I feel like without your help I would have lost a lot of time on this one and gotten discouraged. Perhaps the prompt could be a little clearer about the style of coding desired.

Thank you very much!

1 Like

could you open a github issue with your feedback for this step?

1 Like

I have done so, hopefully correctly ^.^

1 Like

Could someone explain me the logic of this code please

Hi @AlexDRichards !

This issue has already been reported and an PR is in the works to allow for multiple correct answers

1 Like

Here is the logic for this step.

When the user supplies a list of numbers like 4,4,2,5, we want to go through each number in the list and see if the number is already in the counts object or not.

If the number can be found in the object

counts[el]

then add 1 to the existing count

if the number can not be found in the counts object, then set the value of counts[el] to be 1

that is what this portion is doing here

the complete code here says the following

set the value for the key to 1 if the number was not found in the counts obj otherwise add 1 to the existing value

it basically another way to write this

they both do the same thing

you can test this out and see the counts object with this example here

enter in some numbers, click calculate, open up the console to see the counts object

hope that helps

1 Like

Thanks u for the answer. I got it now

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.