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)
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?
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.
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