Building A Static Calculator Step 35

Link to the step:

My code:

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

Don’t know what I’m doing wrong here. Looked at the other forum posts and tried following the help there, with no luck either. I’ve tried multiple versions of my code, such as:

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

…etc. What am I missing? thanks!

Look at the logic in your last line:
If counts[el] is true then counts[el] = counts[el]+=1

There’s too many equals in that expression.

that was only one of the ones I tried. And I agree, there are too many counts[el] there (second one unnecessary)

So what’s wrong with the other codes I tried?

I think it is an issue of how the test are written. They take your function and convert it to a string and parse it, but they can only do so much to make sure it matches the correct format. Your second example does work but will fail the test for not having a ternary which is incorrect.

In these cases, you want to try and be as straight forward as possible. If you look at the if else think how it would look if you directly replaced with a ternary that should otherwise be identical

you mean this code, right?:
array.forEach((el) => counts[el] ? counts[el]+=1 : counts[el]=1)

I don’t understand how that does not have a ternary. We learned earlier in FCC (quoted directly from the site):

The ternary operator is a conditional operator and can be used as a one-line if-else statement. The syntax is: condition ? expressionIfTrue : expressionIfFalse.<

counts[el] is condition
counts[el]+=1 is exp if true
counts[el]=1 is exp if false


I am saying the test FCC wrote are not detecting the ternary you have written, and therefore that code does not pass because it does not think you are using a ternary in the proper place.

So, I was suggesting a way you could alter your code such that you may be aware sometimes the answer needs to be a specific way

I mention the last line because it was the closest. It just has one problem is that when it evaluates to counts[el] = counts[el]+=1. After testing it the syntax does work though… it’s just a bit odd.

thanks @pkdvalis and @caryaharper for your help! I’ve been able to figure it out from the replies here and on another thread. If anyone else finds this and is struggling on this step, I recommend checking out the other thread

1 Like