Created
January 13, 2014 05:44
-
-
Save areagray/8395276 to your computer and use it in GitHub Desktop.
Coderbyte Challenge.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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