This is a challenge for coderbyte. I thought that I would try to do this using a different method to solve it than loops, objects. It has passed, but it is not perfect. Directions for calling:
Let the LetterCountI (str) function accept the passed str parameter and return the first word with the most repeated letters. For example: "Today is the greatest day!" must return more because he has 2 e (and 2 t), and he comes to the point that he also has 2 e. If there are no words with duplicate letters, return -1. Words will be separated by spaces.
function LetterCountI(str){
var wordsAndLetters = {};
var count = 0;
var finalword;
str = str.split(" ");
for(var i = 0; i < str.length; i++){
wordsAndLetters[str[i]] = wordsAndLetters[str[i]] || 0;
}
function countWordLetters(strs){
strs = strs.split("");
var lettercount = {};
for(var i = 0; i <strs.length; i++){
lettercount[strs[i]] = lettercount[strs[i]] || 0;
lettercount[strs[i]]++;
}
return lettercount;
}
for(var words in wordsAndLetters){
wordsAndLetters[words] = countWordLetters(words);
var highestLetterFrequency = wordsAndLetters[words];
for(var values in highestLetterFrequency){
if(highestLetterFrequency[values] > count){
count = highestLetterFrequency[values];
finalword = words;
}
if(count !== 1){
return finalword;
}
}
}
return -1;
}
LetterCountI("today is the greatest day ever!");
, , , . , "",
LetterCountI("toddday is the greatttttest day ever!");
"toddday", "greatttttest". ? , ( "caatt dooog" ), "caatt", , , , ( , !). , , , !
user2755667