Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an “ay”. If a word begins with a vowel you just add “way” to the end.
function translatePigLatin(str) {
var firstVowelIndex =0;
while(str.charCodeAt(firstVowelIndex)!=97 &&
str.charCodeAt(firstVowelIndex)!=101 &&
str.charCodeAt(firstVowelIndex)!=105 &&
str.charCodeAt(firstVowelIndex)!=111 &&
str.charCodeAt(firstVowelIndex)!=117 &&
str.charCodeAt(firstVowelIndex)!=65 &&
str.charCodeAt(firstVowelIndex)!=69 &&
str.charCodeAt(firstVowelIndex)!=73 &&
str.charCodeAt(firstVowelIndex)!=79 &&
str.charCodeAt(firstVowelIndex)!=85){
firstVowelIndex++;
}
if(firstVowelIndex===0){
str = str + "way";
}else{
str = str.substr(firstVowelIndex) + str.substr(0,firstVowelIndex) + "ay";
}
return str;
}
Leave a comment