[FEEDBACK] Pig Latin

Hello guys, since this algorithm actually gave me some headache, i wanted to ask for some feedback on my code:

function translatePigLatin(str) {
  pattern = /[aeiou]/gi;
  vowelPos = pattern.exec(str).index; // return the position of the first vowel found by the regex.exec
  
  if(vowelPos === 0) {
    str = str + "way";
  } else {
    consCluster = str.substr(0, vowelPos);
    str = str.substr(vowelPos) + consCluster + "ay";
  }
  
  return str;
  
}

translatePigLatin("consonant");

That looks like basically what I did. I’m sure there’s more than one way to skin that atcay, but this works.

Now, how does it handle the obscure English words where w is a vowel, like crwth or cwm? :wink:

1 Like

Badly. Really badly.

1 Like