Pig Latin-Solution 4

Tell us what’s happening:
Hello,

Would you please somebody explain why the order of replace() methods is crucial in solution 4 in order to work?

Thanks!

Your code so far


function translatePigLatin(str) {

return str
  .replace(/^[aeiou]\w*/, "$&way")
  .replace(/(^[^aeiou]+)(\w*)/, "$2$1ay");

}

translatePigLatin("consonant");

Challenge: Pig Latin

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin

Because second .replace() removes the first character, which will invoke the first .replace() if the second character is vowel.

1 Like

Thank you so much for the explanation.