Skip to content

Instantly share code, notes, and snippets.

@jwill9999
Created October 10, 2017 16:26
Show Gist options
  • Save jwill9999/7c073d368e971e2ca5a0261806974821 to your computer and use it in GitHub Desktop.
Save jwill9999/7c073d368e971e2ca5a0261806974821 to your computer and use it in GitHub Desktop.
Pig Latin
/*
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.
Input strings are guaranteed to be English words in all lowercase.
*/
function translatePigLatin(str) {
var vowels = ['a', 'e', 'i', 'o', 'u'];
for (var j in str) {
for (var i = 0; i < vowels.length; i++) {
if (str[j] === vowels[i]) {
var index = str.indexOf(vowels[i]);
var extract = str.substr(0, index);
var remainder = str.substr(str.indexOf(vowels[i]));
if (str.charAt(0) === vowels[i]) {
str = remainder + extract + 'way';
} else {
str = remainder + extract + 'ay';
}
return str;
}
}
}
}
translatePigLatin('algorithm');
/*
Test your code here
translatePigLatin("california") should return "aliforniacay".
translatePigLatin("paragraphs") should return "aragraphspay".
translatePigLatin("glove") should return "oveglay".
translatePigLatin("algorithm") should return "algorithmway".
translatePigLatin("eight") should return "eightway".
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment