Here’s a basic solution I came up with and it is supposed to checkout just fine. However, I ran the tests and the last two didn’t.
- Should handle words where the first vowel comes in the end of the word.
- Should handle words without vowels.
Please help me figure this out.
My code so far
function translatePigLatin(str) {
let vowels = ['a', 'e', 'i', 'o', 'u'];
// Word begins with a vowel
if (vowels.indexOf(str[0]) !== -1) {
return str + 'way';
} else if (vowels.indexOf(str[0]) === -1 && vowels.indexOf(str[1]) === -1) {
// Word begins with two-letter consonant cluster
return str.slice(2) + `${str.slice(0, 2)}ay`;
} else if (vowels.indexOf(str[0]) === -1 && vowels.indexOf(str[1]) !== 1) {
// Word begins with one-letter consonant cluster
return str.slice(1) + `${str.slice(0, 1)}ay`;
} else {
// Word with first vowel at the end
return str.slice(str.length - 1) + `${str.slice(0, str.length - 1)}ay`;
}
// Words without vowels
return str + 'ay';
}
console.log(translatePigLatin("california")); // aliforniacay
console.log(translatePigLatin("paragraphs")); // aragraphspay
console.log(translatePigLatin("glove")); // oveglay
console.log(translatePigLatin("consonant")); // onsonantcay
console.log(translatePigLatin("algorithm")); // algorithmway
console.log(translatePigLatin("eight")); // eightway
console.log(translatePigLatin("tha")); // athay
console.log(translatePigLatin("my")); // myay
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.2 Safari/605.1.15
.
Link to the challenge: