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:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
2 things seems strange to me:
I think this part will never be executed:
} else {
// Word with first vowel at the end
return str.slice(str.length - 1) + `${str.slice(0, str.length - 1)}ay`;
you are limited to 2 consonants
What about eg: xyza? Shouldn’t it be axyzay ?
ILM
October 13, 2019, 10:44am
3
Your code works only if the first vowel is one of the first three letters, see what happens with these function calls:
let a = "ibbbbbbbbb"
console.log(translatePigLatin(a)); // ibbbbbbbbbway
a = "bibbbbbbbb"
console.log(translatePigLatin(a)); // ibbbbbbbbbay
a = "bbibbbbbbb"
console.log(translatePigLatin(a)); // ibbbbbbbbbay
a = "bbbibbbbbb"
console.log(translatePigLatin(a)); // bibbbbbbbbay
a = "bbbbibbbbb"
console.log(translatePigLatin(a)); // bbibbbbbbbay
a = "bbbbbibbbb"
console.log(translatePigLatin(a)); // bbbibbbbbbay
a = "bbbbbbibbb"
console.log(translatePigLatin(a)); // bbbbibbbbbay
a = "bbbbbbbibb"
console.log(translatePigLatin(a)); // bbbbbibbbbay
a = "bbbbbbbbib"
console.log(translatePigLatin(a)); // bbbbbbibbbay
a = "bbbbbbbbbi"
console.log(translatePigLatin(a)); // bbbbbbbibbay
Whew! Thanks for your reviews guys… I just worked out a better solution using regex, match() and the substr() methods. This is way better than the first.
function translatePigLatin(str) {
let vowelsRegex = /[aeiou]/gi;
// Word starts with a vowel
if (str[0].match(vowelsRegex)) {
return str + 'way';
} else if (!str.match(vowelsRegex)) {
// Word without a vowel
return str + 'ay';
} else {
// Get the number of consonants before the first vowel and grab the index of the first
let vowelIndex = str.indexOf(str.match(vowelsRegex)[0]);
// Handle words that begins with consonant clusters
return str.substr(vowelIndex) + str.substr(0, vowelIndex) + 'ay';
}
}
1 Like