2018-10-04 14:37:37 +01:00
---
id: aa7697ea2477d1316795783b
title: Pig Latin
challengeType: 5
2019-07-31 11:32:23 -07:00
forumTopicId: 16039
2018-10-04 14:37:37 +01:00
---
## Description
< section id = 'description' >
2020-03-26 18:38:07 -04:00
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.
2018-10-04 14:37:37 +01:00
< / section >
## Instructions
< section id = 'instructions' >
2020-03-26 18:38:07 -04:00
Translate the provided string to Pig Latin. Input strings are guaranteed to be English words in all lowercase.
2018-10-04 14:37:37 +01:00
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
- text: < code > translatePigLatin("california")</ code > should return "aliforniacay".
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(translatePigLatin("california"), "aliforniacay");
2018-10-04 14:37:37 +01:00
- text: < code > translatePigLatin("paragraphs")</ code > should return "aragraphspay".
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(translatePigLatin("paragraphs"), "aragraphspay");
2018-10-04 14:37:37 +01:00
- text: < code > translatePigLatin("glove")</ code > should return "oveglay".
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(translatePigLatin("glove"), "oveglay");
2018-10-04 14:37:37 +01:00
- text: < code > translatePigLatin("algorithm")</ code > should return "algorithmway".
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(translatePigLatin("algorithm"), "algorithmway");
2018-10-04 14:37:37 +01:00
- text: < code > translatePigLatin("eight")</ code > should return "eightway".
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(translatePigLatin("eight"), "eightway");
2019-03-25 11:02:18 -04:00
- text: Should handle words where the first vowel comes in the middle of the word. < code > translatePigLatin("schwartz")</ code > should return "artzschway".
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(translatePigLatin("schwartz"), "artzschway");
2019-03-25 11:02:18 -04:00
- text: Should handle words without vowels. < code > translatePigLatin("rhythm")</ code > should return "rhythmay".
testString: assert.deepEqual(translatePigLatin("rhythm"), "rhythmay");
2018-10-04 14:37:37 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function translatePigLatin(str) {
return str;
}
translatePigLatin("consonant");
```
< / div >
< / section >
## Solution
< section id = '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;
}
```
< / section >