Skip to content

Instantly share code, notes, and snippets.

@areagray
Created January 13, 2014 05:44
Show Gist options
  • Save areagray/8395276 to your computer and use it in GitHub Desktop.
Save areagray/8395276 to your computer and use it in GitHub Desktop.
Coderbyte Challenge.
//Coderbyte Challange.
function LetterCountI(str) {
var words = str.split(' '),
champWord='',
champTotal=0,
champLetter='',
tempTotal,
letterTotal,
letterChamp,
letters=[],
matches=[];
//convert to array of words and loop through
for (var i = 0; i<words.length; i++){
letterTotal=0;
letterChamp='';
//convert to array for letters
letters=words[i].split('');
//loop through each letter and get match total
for (var j=0; j<letters.length; j++){
tempTotal=0;
var re = new RegExp('[' + letters[j] + ']','gi');
tempTotal= words[i].match(re).length;
if (tempTotal > letterTotal){
letterTotal=tempTotal;
letterChamp = letters[j];
}
}
//revise leader board
if (letterTotal>champTotal){
champTotal = letterTotal;
champWord = words[i];
champLetter=letterChamp;
}
}
if (champTotal<2){
//no duplicate letters in words
return -1;
}
else {
return champWord;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment