Intermediate Algorithm Scripting: Pig Latin FEEDBACK

Intermediate Algorithm Scripting: Pig Latin FEEDBACK
0

#1

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");

#2

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.


#3

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


#4

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution.


#5

Thanks for the kind words :slight_smile:


#6

This solution is neat. Love it.