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

**URL:** <https://forum.freecodecamp.org/t/could-use-some-feedback-on-my-solution-intermediate-algorithm-scripting-pig-latin/579215>\
**Category:** I Built This\
**Created:** [December 20, 2022, 2:44pm UTC](https://forum.freecodecamp.org/t/could-use-some-feedback-on-my-solution-intermediate-algorithm-scripting-pig-latin/579215 "2022-12-20T14:44:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![larsonar24](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/larsonar24/32/240209_2.png) [@larsonar24](https://forum.freecodecamp.org/u/larsonar24)\
**Post date:** [December 20, 2022, 2:44pm UTC](https://forum.freecodecamp.org/t/could-use-some-feedback-on-my-solution-intermediate-algorithm-scripting-pig-latin/579215/1 "2022-12-20T14:44:57Z")

</div>

**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**

```javascript
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('')
}

translatePigLatin("consonant");

console.log(translatePigLatin("glove"))

```

**Your browser information:**

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

**Challenge:** Intermediate Algorithm Scripting - Pig Latin

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![tzerio](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tzerio/32/260220_2.png) [@tzerio](https://forum.freecodecamp.org/u/tzerio)\
**Post date:** [December 20, 2022, 4:19pm UTC](https://forum.freecodecamp.org/t/could-use-some-feedback-on-my-solution-intermediate-algorithm-scripting-pig-latin/579215/2 "2022-12-20T16:19:56Z")

</div>

Indeed it’s a bit overcomplicated 😉  
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

---

<div class="post-metadata">

**Author:** ![larsonar24](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/larsonar24/32/240209_2.png) [@larsonar24](https://forum.freecodecamp.org/u/larsonar24)\
**Post date:** [December 20, 2022, 4:40pm UTC](https://forum.freecodecamp.org/t/could-use-some-feedback-on-my-solution-intermediate-algorithm-scripting-pig-latin/579215/3 "2022-12-20T16:40:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [June 21, 2023, 4:41am UTC](https://forum.freecodecamp.org/t/could-use-some-feedback-on-my-solution-intermediate-algorithm-scripting-pig-latin/579215/4 "2023-06-21T04:41:13Z")

</div>

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