Pig Latin - regex evaluates to FALSE instead of TRUE

Tell us what’s happening:

Your code so far


function translatePigLatin(str) {
  let newRegex = /([^aeiou])/gi;

  if (newRegex.test(str[0]) && newRegex.test(str[1])) {
    return str.slice(2) 
    + str[0] 
    + str[1] 
    + "ay";
  } else if (newRegex.test(str[0])) {
    return str.slice(1) 
    + str[0]
    + "ay";
  }

  return str + 'way';
}

translatePigLatin("consonant");
newRegex.test(str[1]) 

This regex evaluates to FALSE for letter l in “glove” :o , i don’t get why…whereas in regexone , it evaluates to true , see : https://regex101.com/r/8u5be3/1 , in dev console , it is false… help is welcome !

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:

remove the g flag.
See here for how the test() method behaves:

But anyway your code with translatePigLatin("strength") results in "rengthstay" - you can certainly do better

1 Like

Thanks :smiley:

I did this :slight_smile:

function translatePigLatin(str) {
  // find a consonnant
  let newRegex = /([^aeiou])/;
  let index = 0;
  while (newRegex.test(str[index])) {
    index++;
  }
  if (index > 0) {
    return str.slice(index) 
      + str.slice(0,index) 
      + "ay";
    }
  return str + 'way';
}