# Code Challenge: Recurring Letters

This is the fourth post in a series of at least 10 JavaScript code challenges. You can find the others here.

This one was a bit of a doozy! I originally read this challenge three weeks ago and gave it a try. I couldn’t get it working so I moved on to another after an hour of frustration. Reflecting back on the “Second Greatest Length” challenge, I had a little epiphany of how it could work.

Challenge: Given a string, the function `highestRecurringLetter` should return the word in that string that contains the highest recurring letter. For example, if given “Trees are great,” the function should return “Trees” because the letter ‘e’ occurs twice. If no letter occurs more than once in a word (“This is fun”), the function should return the last word in the string. Assume that the string has at least one word, and words are separated by spaces.

Test cases

`highestRecurringLetter("Soccer is fun")` = ‘Soccer’;
`highestRecurringLetter("Recurring challenges are fun")` = ‘Challenges’;
`highestRecurringLetter("I love them")` = ‘Them’;

Here’s my solution minimized. I want to see yours though. If you give this a shot, let me know and post a link to your solution in the comments! Otherwise, just let me know what I could’ve done better!

[code language=”javascript” collapse=”true”]
<script>
var highestRecurringLetter = function(string) {
// Split the string into separate words
var words = string.split(‘ ‘);
// Set variables for highest recurring letter and the current leader
var highestRecurring = 0;
var bestWord;
// Loop over the words
for (var i = 0; i < words.length; i++) {
// For each word, split it into individual letters
var letters = words[i].split(”)
// Loop over each letter
for (var t = 0; t < letters.length; t++) {
// Compare the letter to other letters in the string
for (var x = 0; x < letters.length; x++) {
// If the lowercase version of the letters match
if (letters[t].toLowerCase() == letters[x].toLowerCase()) {
}
}
// Compare the leader value to the current highestRecurring
// If it’s larger or equal to
// Replace the highestRecurring value
// Set the best word to the word including this letter
bestWord = words[i];
// Set the leader back to zero
// Otherwise, set the leader back to zero
} else {
}
}
}
// Write out the information to the page
var results = "The string provided was: " + string;
results += "<br>" + "The word with the most recurring letters is: ‘" + bestWord;
results += "’. It has a letter that occurs " + highestRecurring + " times."
document.write(results)
};

highestRecurringLetter(‘This is a lot of fun’);
</script>
[/code]

It looks a bit involved, but it really just involves quite a few loops. Here are the basics in sudo code:

1. Split the string into individual words
2. Setup variables to store the highest recurring and then the leader. The “`highestRecurring“` will store the highest recurring letter at any moment. “`leader“` will store the value of the current letter we’re counting.
3. Loop over each word and split the word into individual letters
4. For each letter in a word, loop over the other letters in the word. If the lowercase value of the letters match, increment the “`leader“` variable.
5. Compare “`leader“` to “`highestRecurring“`. If it’s equal to or larger replace “`highestRecurring“` with `leader` and set leader back to 0. Otherwise, just set `leader` back to 0.
6. Write the information to the page.