Skip to content

Instantly share code, notes, and snippets.

@relaxedtomato
Created March 3, 2015 14:26
Show Gist options
  • Save relaxedtomato/a7673d3691ecd52e6391 to your computer and use it in GitHub Desktop.
Save relaxedtomato/a7673d3691ecd52e6391 to your computer and use it in GitHub Desktop.
pig_latin
//if begins with a vowel, add 'ay' to the end
//consonant sounds, move it to the end and add 'ay'
function translate(str){
var strArr = str.split(" ");
var result = [];
strArr.forEach(function(word){
var wordArr = word.split("");
console.log(wordArr);
var shiftToEnd = '';
for(var i = 0;i<wordArr.length;i++){
if((wordArr[i]==='a')||(wordArr[i]==='e')|(wordArr[i]==='i')|(wordArr[i]==='o')|(wordArr[i]==='u')){//vowel
if((shiftToEnd[shiftToEnd.length-1]==='q')&&(wordArr[i]==='u')){
result.push(wordArr.slice(i+1,word.length).join("")+shiftToEnd.slice(0,shiftToEnd.length)+'uay');
}
else{
result.push(wordArr.slice(i,word.length).join("")+shiftToEnd+'ay');
}
break;
}
else{ //storing consonants
shiftToEnd += wordArr[i];
}
}
});
return result.join(" ");//appleay //ananabay 'b'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment