PigLatin - can't pass the last test

I tested my code with three different kinds of word examples. Word started with vowel, word started with consonant, and word without any vowel in it. When I try my codes in a code editor I get the result as it is expected by freeCodeCamp’s guideline, but unfortunately when I paste my code into the freeCodeCamp’s editor, I still can’t pass the last test. Will anyone please help me to correct my wrong. Thank you.

function translatePigLatin(str) {
      
  let pattern1 = /(^[aeiou])(\w+)/; // Regex pattern for vowel followed by consonant.
  let pattern2 = /(\w+?)(?=[aeiou])(\w+)/; // Regex pattern for word started with consonant.
  let tester1 = pattern1.test(str); // true for word starts with vowel
  let tester2 = pattern2.test(str); // true for word starts with any letter and followed by vowel 
      
  if (tester1) {
    // for word starts with vowel
    return str.replace(pattern1, '$1$2way');
  } else if (tester2) {
    // for word starts with any letter and followed by vowel
    return str.replace(pattern2, '$2$1ay');
  } else {
    // for word outside of the above two categories
    return str.slice(1) + str[0] + 'ay';
  }
  
}

console.log(translatePigLatin('consonant')); // onsonantcay
console.log(translatePigLatin('eight')); // eightway
console.log(translatePigLatin('bcdfgh')); // cdfghbay

if this is what your code does, that is the wrong returned value, it should be bcdfghay, the whole word is the consonant cluster, so you just add ay at the end

2 Likes