Skip to content Skip to sidebar Skip to footer

Get Count Of Most Repeated Letter In A Word

I am trying to get the count of the most repeated letter in a word function GreatestCount(str) { var count = {} for (var i = 0 ; i

Solution 1:

Because

count[char] += 1

is equal to

count[char] = count[char] + 1

and the first time the code is run, count[char] is undefined so it's pretty much the same as

undefined + 1// which is NaN

The working version circumvents this case by safely adding with 0 using || operator.

Solution 2:

Initially, count is an empty object, so it doesn't have the char property. Therefore, count[char] returns undefined.

And undefined + 1 produces NaN.

Therefore, you must inititialize it to 0 in order to make it work properly.

Solution 3:

You need to initialize your count[char] to zero before incrementing it.

Solution 4:

On first occurrence count[char] is undefined and undefined += 1 !== 1

Solution 5:

As other said your variable is not initialize in the beginning so count[char] +=1 is not working, but when you do (count[char] || 0) you actually tell them that you want to set 0 if the variable is "false". False could mean undefined, NaN, 0.

Post a Comment for "Get Count Of Most Repeated Letter In A Word"