--- id: aa7697ea2477d1316795783b title: Pig Latin isRequired: true challengeType: 5 --- ## Description
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. Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
## Instructions
## Tests
```yml tests: - text: translatePigLatin("california") should return "aliforniacay". testString: 'assert.deepEqual(translatePigLatin("california"), "aliforniacay", "translatePigLatin("california") should return "aliforniacay".");' - text: translatePigLatin("paragraphs") should return "aragraphspay". testString: 'assert.deepEqual(translatePigLatin("paragraphs"), "aragraphspay", "translatePigLatin("paragraphs") should return "aragraphspay".");' - text: translatePigLatin("glove") should return "oveglay". testString: 'assert.deepEqual(translatePigLatin("glove"), "oveglay", "translatePigLatin("glove") should return "oveglay".");' - text: translatePigLatin("algorithm") should return "algorithmway". testString: 'assert.deepEqual(translatePigLatin("algorithm"), "algorithmway", "translatePigLatin("algorithm") should return "algorithmway".");' - text: translatePigLatin("eight") should return "eightway". testString: 'assert.deepEqual(translatePigLatin("eight"), "eightway", "translatePigLatin("eight") should return "eightway".");' - text: Should handle words where the first vowel comes in the end of the word. testString: 'assert.deepEqual(translatePigLatin("schwartz"), "artzschway", "Should handle words where the first vowel comes in the end of the word.");' - text: Should handle words without vowels. testString: 'assert.deepEqual(translatePigLatin("rhythm"), "rhythmay", "Should handle words without vowels.");' ```
## Challenge Seed
```js function translatePigLatin(str) { return str; } translatePigLatin("consonant"); ```
## Solution
```js 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; } ```