* Update description of tests pig-latin.english.md The last two tests "Should handle words where the first vowel comes in the end of the word." and "Should handle words without vowels." are too vague without examples of what is meant by that. * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin.english.md Co-Authored-By: beansprout <christinegierer@gmail.com>
3.1 KiB
3.1 KiB
id, title, isRequired, challengeType
id | title | isRequired | challengeType |
---|---|---|---|
aa7697ea2477d1316795783b | Pig Latin | true | 5 |
Description
Instructions
Tests
tests:
- text: <code>translatePigLatin("california")</code> should return "aliforniacay".
testString: assert.deepEqual(translatePigLatin("california"), "aliforniacay", '<code>translatePigLatin("california")</code> should return "aliforniacay".');
- text: <code>translatePigLatin("paragraphs")</code> should return "aragraphspay".
testString: assert.deepEqual(translatePigLatin("paragraphs"), "aragraphspay", '<code>translatePigLatin("paragraphs")</code> should return "aragraphspay".');
- text: <code>translatePigLatin("glove")</code> should return "oveglay".
testString: assert.deepEqual(translatePigLatin("glove"), "oveglay", '<code>translatePigLatin("glove")</code> should return "oveglay".');
- text: <code>translatePigLatin("algorithm")</code> should return "algorithmway".
testString: assert.deepEqual(translatePigLatin("algorithm"), "algorithmway", '<code>translatePigLatin("algorithm")</code> should return "algorithmway".');
- text: <code>translatePigLatin("eight")</code> should return "eightway".
testString: assert.deepEqual(translatePigLatin("eight"), "eightway", '<code>translatePigLatin("eight")</code> should return "eightway".');
- text: Should handle words where the first vowel comes in the middle of the word. <code>translatePigLatin("schwartz")</code> should return "artzschway".
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. <code>translatePigLatin("rhythm")</code> should return "rhythmay".
testString: assert.deepEqual(translatePigLatin("rhythm"), "rhythmay");
Challenge Seed
function translatePigLatin(str) {
return str;
}
translatePigLatin("consonant");
Solution
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;
}