Pig Latin confusion

Tell us what’s happening:

I don’t fully understand the third case, in which the first vowel comes in the end of the string.

translatePigLatin("glove") //should return "oveglay".

It’s confusing because it’s not returning the string backwards as I first thought. What exactly am I supposed to do if the case is true?

What it seems to be doing is removing each letter (from the start of the string) that isn’t a vowel until it finds one, in this case it removes “g” and “l” then finds “o” (a vowel). After that it takes those letters and add them to the end of the string along with the word “ay”

Am I correct here?, the challenge doesn’t mention any of that though and with my current code I’m failing the last case, not sure why.

Your code so far


function translatePigLatin(str) {
  const vowels = ["a", "e", "i", "o", "u"]
  for (let i = 0; i < vowels.length; i++) {
  	if(str.startsWith(vowels[i])) {
    	return str + "way"
    } else if (str.endsWith(vowels[i])) {
    	return str
    }
  }

 const arr = str.split("")
 const firstLetter = arr.shift();
 arr.push(firstLetter + "ay")
 return arr.join("")
}

translatePigLatin("consonant");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36.

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

Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an “ay”.

Does that mean I need an array with the list of all these “consonant cluster”?

I suck when it comes to these grammar terms so I totally ignored that part :sweat_smile:

consonant cluster = many consonants (not ‘aeiou’) stuck together (i.e. ‘gl’, ‘str’).

And how do you imagine creating “an array with the list of all these “consonant cluster””?

But ‘ca’ is not a consonant cluster and it’s still treating it as if it were one in the first case (california), why?

translatePigLatin("california") should return “aliforniacay”.

Pig Latin takes the first consonant (or consonant cluster) of an English word,

c-alifornia

moves it to the end of the word

alifornia-c

and suffixes an “ay”.

alifornia-c-ay

1 Like

Oh I see, this post just made realize that I’m really bad with grammar terms. Thanks.

As for the list of consonant cluster, I saw a list and there are about 32 in the English language. Not sure if that would be the best way to solve this challenge but it’s the first thing that comes to my mind, I need way to check if the string starts with a consonant cluster (i.e., gl, bl, etc)

That would not be my first choice. Just work out the algorithm out on paper and test the steps on some of the test cases to see if your algorithm will work. Then think about how to translate your steps into code.

Yeah I’m trying to figure out another approach, having to type the entire list of consonant cluster into an array does not seem very good.

Think about how you would deal with this problem if you could not see the entire word at the beginning, but could only see each letter as you come across them. So basically, you will know what the current letter is (if it is a vowel or consonant) at each step of the way and you will also know which letters you have already seen. See if you can use that to your advantage. Write one of the words on a piece of paper and start by covering up all but the first letter, then all but the first two letters, and so on.