Pig Latin's solution, is it bad?

Pig Latin's solution, is it bad?
0

#1

Hi!

I had been stuck with this exercise a little bit. So I checked possible answers on the forum but they were a little bit confusing for me. I reviewed all the regexp and also used regexp101 website to experiment, and solved the exercise. But I see other solutions that are solved in a couple of codes, mine is long though. I was wondering. Is my code too “bad”? Is it considered acceptable code by advanced coders?

function translatePigLatin(str) {
  var regExp = /(^[^aeiou]+)([aeiou])(.*)/;
  var regExp2 = /(^[aeiou]+)(.+)/;
  var regExp3 = /([^aeiou]+)/;
  if (regExp.test(str)){
    var newString = str.replace(regExp, '$2$3$1ay');
  } else if (regExp2.test(str)){
    var newString = str.replace(regExp2,'$1$2way');
  } else if (regExp3.test(str)){
    var newString = str.replace(regExp3,'$1ay');
  }
  return newString;
}

translatePigLatin("consonant");

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin/


#2

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.

I see a lot of repeated code. See if you can make your solution more DRY (Do Not Repeat Yourself).


#3

It’s a good starting place to clean up your code, but if you leave it like this it’s kind of bad.

  • I find those regexps too complicated for the given job.
  • The code doesn’t convey any information until one parses the regexps. Also, each regexp has no meaningful name on what it represent.

If you see a same task is done in a shorter and more readable way, then you should try to understand that solution rather than finding reasons to keep your code.

In this problem, I don’t see any reason to use more than one regexp.