Javascript PigLatin, why doesn't my code work with "glove"?

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

Are you sure, you are checking the length of matched string?

2 Likes

Oh my god!
Thank you so much, I just edited into regexcons[0].length and it worked like a charm.

I guess I have to familiarize a lot more with these functions and what they return!

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.