Skip to main content

--description--

Pig Latin is a way of altering English Words. The rules are as follows:

- If a word begins with a consonant, take the first consonant or consonant cluster, move it to the end of the word, and add ay to it.

- If a word begins with a vowel, just add way at the end.

--instructions--

Translate the provided string to Pig Latin. Input strings are guaranteed to be English words in all lowercase.

--hints--

translatePigLatin("california") should return the string aliforniacay.

assert.deepEqual(translatePigLatin('california'), 'aliforniacay');

translatePigLatin("paragraphs") should return the string aragraphspay.

assert.deepEqual(translatePigLatin('paragraphs'), 'aragraphspay');

translatePigLatin("glove") should return the string oveglay.

assert.deepEqual(translatePigLatin('glove'), 'oveglay');

translatePigLatin("algorithm") should return the string algorithmway.

assert.deepEqual(translatePigLatin('algorithm'), 'algorithmway');

translatePigLatin("eight") should return the string eightway.

assert.deepEqual(translatePigLatin('eight'), 'eightway');

Should handle words where the first vowel comes in the middle of the word. translatePigLatin("schwartz") should return the string artzschway.

assert.deepEqual(translatePigLatin('schwartz'), 'artzschway');

Should handle words without vowels. translatePigLatin("rhythm") should return the string rhythmay.

assert.deepEqual(translatePigLatin('rhythm'), 'rhythmay');

--seed--

--seed-contents--

function translatePigLatin(str) {
return str;
}

translatePigLatin("consonant");

--solutions--

function translatePigLatin(str) {
if (isVowel(str.charAt(0))) return str + "way";
var front = [];
str = str.split('');
while (str.length && !isVowel(str[0])) {
front.push(str.shift());
}
return [].concat(str, front).join('') + 'ay';
}

function isVowel(c) {
return ['a', 'e', 'i', 'o', 'u'].indexOf(c.toLowerCase()) !== -1;
}