Tell us what’s happening:
Hi there, I was wondering why my code doesn’t work with the word “glove”.
I tried checking with console.log and found out that while the regex is correctly recognized (“gl”), its length is 1 instead of 2, so instead of having “oveglay” I end up with “loveglay”, without trimming the first “l”, but correctly adding “glay” at the end of the word.
Why is this happening?
Your code so far
function translatePigLatin(str) {
//checks if words start with consonants or vowels and stores the value
let regexcons = str.match(/^[^aeiou]+/);
let regexvow = str.match(/^[aeiou]+/);
//pigLatin with consonants
if (regexcons !== null){
let pigged = str.slice(regexcons.length);
return pigged + regexcons + "ay";
}
else {
//pigLatin with vowels
if (regexvow !== null){
return str + "way"
}
}
}
console.log(translatePigLatin("consonant"));
console.log(translatePigLatin("california"));
console.log(translatePigLatin("algorithm"));
console.log(translatePigLatin("glove"));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0
Challenge Information:
Intermediate Algorithm Scripting - Pig Latin