Hello!
Stuck in this Pig Latin challenge, (really hate working with regex btw), though I’m not sure what is wrong with my code right now, as it actually passes all tests except the “Should handle words without vowels.”, though when I try words without vowels it works perfectly.
Here is the code:
function translatePigLatin(str) {
const vowelRegex = /(^[aeiou])/;
const consonantRegex = /^([^aeiou][b-df-hj-np-tv-z]*)(.*)/;
const noVowelRegex = /^([b-df-hj-np-tv-z])([b-df-hj-np-tv-z]+)$/;
console.log(vowelRegex.test(str));
console.log(noVowelRegex.test(str));
console.log(consonantRegex.test(str));
if (vowelRegex.test(str)){
str = str.concat("way")
console.log(str + " vowel Test");
return str;
}
if (noVowelRegex.test(str)){
let arr = str.split(noVowelRegex).filter((item) => item !== "");
console.log(arr + " noVowel test");
arr = arr[1].concat(arr[0] + "ay")
console.log(arr);
return arr;
} else if (consonantRegex.test(str)){
let arr = str.split(consonantRegex).filter((item) => item !== "");
console.log(arr + " consonant test");
arr = arr[1].concat(arr[0] + "ay");
console.log(arr);
return arr;
}
Please don’t mind the console.logs, makes it easier for me to know what I’m doing. Thank you all!
false
VM154:10 true
VM154:11 true
VM154:22 r,hythm noVowel test
VM154:24 hythmray
This is not the correct output, though, for a word without a vowel
You need to only add the “ay” to the end in this scenario, but your second if statement instead throws the first letter off the top and on to the end. Leave the first letter where it is, concat the “ay” to the end, and join the array back to a string.
I was able to pass the test using your code, with the changes above.
1 Like
Hummm yeah, did the changes and it worked, but those instructions are not written anywhere in the challenge.
Intermediate Algorithm Scripting: Pig Latin
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.
1 Like
Yep, could be a bit clearer! Although if you think about it, moving the consonant cluster of a word without a vowel to the end of the same word, would just be repeating the word, then adding an “ay”…
But, it was not intuitive for me either when I was doing the challenge. Also, I was never one of the fluent in Pig Latin kids in elementary school. So I share your pain.
1 Like
That makes 2 of us, I’m tempted to skip this challenge after failing several tests and realizing that I’d probably need regex to solve this or do it the “wrong” way, basically cheating by creating a list of consonant cluster array.
I’m really not interested in regular expressions honestly.
1 Like