Intermediate Algorithm Scripting: Pig Latin FEEDBACK

So i was messing around with the pig latin challenge and got it working. While checking the ‘get a hint’ section later, I noticed I had a bit of a different approach. I’m curious what you think of it :wink:

function translatePigLatin(str) {
  var regex = /([^aeiou]+)(\w+)/;
  var testRegex = /^[aeiou]/;
  var startWithVowel = testRegex.test(str);
  var hasVowel = /[aeiou]/.test(str);

  if (!startWithVowel && hasVowel) {
    return str.replace(regex, '$2$1') + 'ay';
  } else if (startWithVowel && hasVowel) {
    return str + 'way';
  } else {
    return str + 'ay';
  } 
}

translatePigLatin("qwfgt");
1 Like

If it works, it’s nice and well done!

You could’ve also used slice and for loop, map and filter…etc.

The regex method is better in my opinion, but it is a little bit difficult to read for those who do not master it.

looks good! Nice to see someone on here with a good grasp of regex!

Thanks for the kind words :slight_smile:

This solution is neat. Love it.

1 Like