Skip to content

Instantly share code, notes, and snippets.

@sedera-tax
Created February 28, 2016 11:55
Show Gist options
  • Save sedera-tax/8680f669bc0fa0ec141a to your computer and use it in GitHub Desktop.
Save sedera-tax/8680f669bc0fa0ec141a to your computer and use it in GitHub Desktop.
Translate the provided string to pig latin. 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 translate(str) {
var length = str.length;
var tab = [];
for(var i=0; i<length; i++){
var first = str.charAt(i);
//voyelle
if(first === "a" || first === "e" || first === "i" || first === "o" || first === "u" || first === "y"){
if(i === 0){
str = str + "way";
}
else{
str = str.substring(i) + tab.join("") + "ay";
}
break;
}
//consonne
else{
tab.push(first);
}
}
return str;
}
translate("consonant");
translate("glove");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment