Intermediate Algorithm Scripting - Pig Latin

Need your opinion

Hi guys, here's my answer for the intermediate algorithm. Can any of you guys give me some input regarding my code? Thank you. love you all!
function translatePigLatin(str) {
  let consonantRegex = /^[bcdfghjklmnpqrstvwxys]+/i;
  let theConsonant = str.match(consonantRegex) == null? [[]]:str.match(consonantRegex)

  if(theConsonant[0].length > 0){
    return str.slice(theConsonant[0].length, str.length) + str.slice(0,theConsonant[0].length) + "ay"
    return str+"way"


Challenge: Intermediate Algorithm Scripting - Pig Latin

Link to the challenge:

I fixed your post - the spoiler tags have to go outside the backticks…

It looks pretty good. If I were to put on my picky hat…

The two lets should be consts.

The first one might want to be outside the function so it doesn’t get reallocated every run - but maybe that’s being too picky, and it may depend. The last consonant is an “s”? This won’t work for “zebra”. Wouldn’t it be easier to list the vowels?

str.match(consonantRegex) == null

Don’t use ==, always use ===. I believe there is a possibility that there exists in the wild a situation where == is better, I just think there is a better chance of catching Bigfoot.

Also, the type to which that ternary evaluates is weird - they’re too different things. That seems odd to me. I’d just make it null or something.

I might just do a return [ternary expression] for the end, but that’s subjective.

But still, good work, you solved the challenge. I’m just being nitpicky, like if this were a code review at work.

Have fun on the next one.

1 Like

thank you very much for the reply, i’ll be asking these types of question more and more in the future

Sounds like a plan for success.

1 Like