Intermediate Algorithm Scripting - Pig Latin

paragraphs & california should work?
while i know this isn’t valid solution since it doesn’t handle words without vowels, but from what I understand it should handle words like paragraphs and California.
it is handling the rest of the tests but for these 2 words it is acting weirdly.
I’m trying to understand how these dollar signs work with regex and replace so let me know :slight_smile:
thanks!

  **Your code so far**
function translatePigLatin(str) {
let result = "";
if(str[0] == 'a' || str[0] == 'e' || str[0] == 'i' || str[0] == 'o' || str[0] == 'u') {
 result = str.concat("way");
}
else {
 result = str.replace(/([a-zA-Z]+)([a||e||i||o||u])([a-zA-Z]+)/, "$2$3$1ay")
}

return result;
}
console.log(translatePigLatin("paragraphs"));
translatePigLatin("consonant");
  **Your browser information:**

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

Challenge: Intermediate Algorithm Scripting - Pig Latin

Link to the challenge:

what do you want to do with this regex?

it can work, but does the regex do what you want?

as i understand it should first find consonants, until it finds a vowel (or vowels but in the case I didn’t add the +) and then the rest of the consonants, in this order and then switch them like this: vowel(s)-lettersaftervowel-lettersbeforevowel+ay
but now I realize that I should’ve excluded the vowels from the first section which includes the entire alphabets thus why it doesn’t take the letters before the first vowel as the 1st part ($1), correct?

You can use a tool like https://regex101.com/ to test your regex (set Flavor to ECMAScript on the left for JavaScript regex)

Also your second capture group doesn’t do what you want (even if it’s much less noticeable in this case, as you never use | in the string), see:
image

1 Like

thanks a lot, specially for the fast answers :slight_smile:

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