Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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'];
var strArr = str.split(''); //split the string into an array
//walk through the string array and test for vowels.
for(var i = 0; i<strArr.length; i++){
//if the letter is a vowel create the new string
//if the first letter is a vowel just add 'way' to the end.
if(i == 0){
return str + 'way';
//otherwise create two substrings one from the point of the vowel to the end of the string
//the second from the beginning to the point of the vowel and then add 'ay to the end.
return str.substr(i, strArr.length-1) + str.substr(0, i) + 'ay';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment