There are a couple of similar questions, however, I am not looking to search for a specific character or letter or find the first word with repeated letters. Also, I want to do this in straight javascript.
I am trying to build a function that takes a string as input, loops over every word in the string and returns the word with the most repeated letters. If there are multiple words with the same number of repeated letters (whether 1 or more) I want to return multiple words.
Here's what I have so far, but it is returning all words in the string, rather than only the one(s) with the most repeated letters. I'm having trouble figuring out how to only return the word(s) with the highest repeat count:
function repeatedLetterCounter(str) {
str = str.toLowerCase();
var wordArray = str.split(" ");
var results = [];
for (var i = 0; i < wordArray.length; i++) {
var countNew = 0;
var count = 0;
var word = wordArray[i];
for (var a = 0; a < word.length; a++) {
var letter = word[a];
for (var b = a + 1; b < word.length; b++) {
var nextLetter = word[b];
if (letter === nextLetter) {
countNew += 1;
}
}
}
if (countNew > count) {
count = countNew;
results.push(wordArray[i]);
} else if (countNew === count) {
results.push(wordArray[i]);
}
}
return results;
}
console.log(repeatedLetterCounter("No, Bob ran across the Mississippi."));
Here is my fiddle.
I will not produce new code from scratch, but take your code and show where the issues are:
Look at this code, where I hide some parts:
for (var i = 0; i < wordArray.length; i++) {
var count = 0;
// two nested loops open and close here //
// ...
if (countNew > count) {
count = countNew;
results.push(wordArray[i]);
// ...
}
}
This code shows that in each iteration you reset count to zero, so the if
at the end will always be true. As a consequence you get all words in your result.
When you increase countNew you keep increasing it even when you are already checking the next character, so in the end it will total the number of all letter repetitions in the word you are looking at, without distinction.
You currently use 2 count variables, but you actually need 3:
function repeatedLetterCounter(str) {
str = str.toLowerCase();
var wordArray = str.split(" ");
var results = [];
var count = 0;
for (var i = 0; i < wordArray.length; i++) {
var word = wordArray[i];
var countThisWordsBestLetter = 0;
for (var a = 0; a < word.length; a++) {
var countLetter = 0;
var letter = word[a];
for (var b = a + 1; b < word.length; b++) {
var nextLetter = word[b];
if (letter === nextLetter) {
countLetter += 1;
}
}
if (countLetter > countThisWordsBestLetter) {
countThisWordsBestLetter = countLetter;
}
}
if (countThisWordsBestLetter > count) {
// forget any words we gathered before:
results = [];
}
if (countThisWordsBestLetter >= count) {
count = countThisWordsBestLetter;
results.push(wordArray[i]);
}
}
return results;
}
console.log(repeatedLetterCounter("No, Bob ran across the Mississippi."));
// for this snippet only:
document.write( JSON.stringify(repeatedLetterCounter("No, Bob ran across the Mississippi.")));