Could use some feedback on my solution: Intermediate Algorithm Scripting - Pig Latin

Tell us what’s happening:
To start, this challenge took me a bit longer than I originally anticipated (roughly 30 minutes) . I have a solution here, but I don’t feel confident in it. Am I overcomplicating this / Is there too much going on in the code?

Your code so far

function translatePigLatin(str) {
  let splitStr = str.split('')
  let firstVowel = str.indexOf(str.match(/[aeiou]/))

  return str.match(/[aeiou]/g) === null
    ? str.concat('ay')
    : str.match(/^[aeiou]/)
    ? str.concat('way')
    : splitStr.concat(splitStr.splice(0, firstVowel), 'ay').join('')



Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Intermediate Algorithm Scripting - Pig Latin

Link to the challenge:

Indeed it’s a bit overcomplicated :wink:
The name of firstVowel is misleading, because it’s not the vowel itself but the index of the first vowel.

Your first case is probably a remain from earlier attempts, because it works still the same without it (think about why it’s not a separate case)

You calculated firstVowel(index), but then still used str.match(/^[aeiou]/) in different variations - for example these cases could be easier to read with firstVowelIndex === 0 (=starts with vowel). With that there is not need to think about what that regex is looking for or what did you think why the global flag was needed (str.match(/[aeiou]/g))

slice exists for strings too, might be prettier to directly use the string instead of chaining all these array methods

1 Like

The global flag was left in on accident, to my shame. The declaration of the firstVowel variable took place in the final stage of writing the code to solve for “glove”, and as you noted, I definitely should have omitted the repeated use of regex once I declared that variable.

I’m still very new to coding (just returned to it recently after nearly half a year), so I really appreciate the advice.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.