Created
June 29, 2015 21:43
-
-
Save markjanzer/24f35389628b2ca49b60 to your computer and use it in GitHub Desktop.
LetterCountI
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
function LetterCountI(str) { | |
str = str.split(" "); | |
var answer = ""; | |
var mostRep = 1; | |
for (i = 0; i < str.length; i++) | |
if (eachWord(str[i]) > mostRep) { | |
mostRep = eachWord(str[i]); | |
answer = str[i]; | |
} | |
if (mostRep === 1) | |
return -1; | |
else | |
return answer; | |
} | |
function eachWord(string) { | |
string = string.toLowerCase().split("").sort(); | |
var counter = 1; | |
var most = 1; | |
for (i = 1; i < string.length; i++) { | |
if (string[i] === string[i - 1]) | |
counter++; | |
else | |
if (counter > most) { | |
most = counter; | |
counter = 1; | |
} | |
if (i === string.length - 1) | |
if (counter > most) | |
most = counter; | |
} | |
return most; | |
} | |
console.log(LetterCountI("heey brew")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment