Skip to content

Instantly share code, notes, and snippets.

@vinayverghese
Last active August 23, 2018 19:10
Show Gist options
  • Select an option

  • Save vinayverghese/de9524503c382462f3ebd6097322eae3 to your computer and use it in GitHub Desktop.

Select an option

Save vinayverghese/de9524503c382462f3ebd6097322eae3 to your computer and use it in GitHub Desktop.
wordsWithRepeatedChars.js
function wordsWithRepeatedChars(str) {
var list = [];
//split string into words based on spaces and count repeated characters
str.toLowerCase().split(" ").forEach(function(currentWord){
var lastLetter = "";
var alreadyAdded = false;
//split word into characters and add to the list only if a letter repeats
currentWord.split('').forEach(function(letter){
if (letter == lastLetter && !alreadyAdded) {
list.push(currentWord);
alreadyAdded = true;
}
lastLetter = letter;
});
});
return list;
}
console.log(wordsWithRepeatedChars("aaa bbb abcbc"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment