I'm trying to use objects as a counter in JavaScript, to find the most repeated letter in a word, but somehow the function is not working as I intend to. For example, when I call findMaxRepeatCountInWord('expected')
, the letterCount object ends up with a value of { e: 1, x: 1, p: 1, c: 1, t: 1, d: 1 }
. What's wrong with my code? Shouldn't e has a value of 3?
My code:
function findMaxRepeatCountInWord(word){
var letterCount = {};
word.split('').map(function(v){
if(letterCount[v] != true){ letterCount[v] = 1 }
else(letterCount[v] ++)
});
return Object.values(letterCount).sort((a,b) => b-a)[0]
}
findMaxRepeatCountInWord('expected')