Pig Latin Challange: recursive attempt logs desired outcome but not passing the test

This is my attempt for Pig Latin challenge. It returns the desired outcome in the console log, as per the challenge requirement. However, my code just won’t pass, except for the input string begins with a vowel. Is there anything I did it wrong, here?

const vowel = ['a', 'e', 'i', 'u', 'o']

function translatePigLatin(str, marker) {
  let arr = str.split("")

  if (vowel.includes(str[0])) {
    if (marker === true) {
      arr.push("ay")
      return arr.join('')
    } else {
      arr.push("way")
      return arr.join('')
    }
  } else if (arr.every(a => !vowel.includes(a))) {
    arr.push("way")
    return arr.join('')
  }

  let x = arr.splice(arr[0],1)
  arr.push(x)
  translatePigLatin(arr.join(''), true)
}

Challange: Pig Latin
Link to the challange: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin

The fact you made a recursion is not really helping understanding what the code is doing compared to what it’s supposed to do…

Though including a bunch of console.logs() to check where your code is doing what reveals the issue (something you should try):
If the first letter is no vowel, your function doesn’t have a return value. You need to return the recursive call.

After that, you did one of the tasks wrong, which you’ll notice once the recursion works.

Also, don’t do recursions unless you have to. It’s just a bad idea because they are hard to follow and debug.

Thank you @Jagaya.

Anyway, I remove the console.logs() from my code for this post.



I'll consider to use non-recursive solution too. My attempt is not the most efficient recursive solution, of course. But, for now I just want to understand what's wrong with the code.

I’ve specified 2 stop conditions for the recursion, which are:

  1. If the string begins with a vowel, re-evaluate if it has marker set to true.
  if (vowel.includes(str[0]))
  • If marker === true (original string has been altered by splice method):
    if (marker === true) {
      arr.push("ay")
      return arr.join('')
    }
  • If marker is not equal true (original string has NOT been altered):
    else {
      arr.push("way")
      return arr.join('')
    }
  1. If the string doesn’t contain any vowels, then just add “way” into it.
  else if (arr.every(a => !vowel.includes(a))) {
    arr.push("way")
    return arr.join('')
  }


I thought that these stop conditions should be enough (?), so the recursion will only be used for the string which contain both vowel(s) and consonant(s) but NOT begins with a vowel.

So, let’s take an example I have the "california" string. If I call translatePigLatin("california")–because it doesn’t met the stop condition–it will go to this line:

  let x = arr.splice(arr[0],1)
  arr.push(x)
  translatePigLatin(arr.join(''), true)

The string (arr) now will become "aliforniac" if we join it.
The recursion will use this "aliforniac" plus true as parameters. Since "aliforniac" begins with a vowel, it will stop on the first stop condition and eventually returns "aliforniacay" (since the marker set to true). At least it is what is showed on the console.log(). So I can’t figure out what is the wrong part in the code …

Will you please mention where the problem lies?

There is a small missunderstanding, sorry.
First, I didn’t mean you should explain the code - I already got that ^^°
Second the solution: you need to return translatePigLatin(arr.join(‘’), true)
Reason: Right now the “outer” function calls the inner function, but while the inner function returns a value, the outer function just has it sitting around.

For the other error: the elsi-if adds the wrong letters.

1 Like

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