Pig Latin - Cannot pass last two tests

Tell us what’s happening:
I can pass the first five tests:

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

  2. translatePigLatin("paragraphs") should return “aragraphspay”.

  3. translatePigLatin("glove") should return “oveglay”.

  4. translatePigLatin("algorithm") should return “algorithmway”.

  5. translatePigLatin("eight") should return “eightway”.

But I cannot pass the last two:

  1. Should handle words where the first vowel comes in the end of the word.

  2. Should handle words without vowels.

I tried removing vowels from the first five tests and it looks to me like my code does handle words without vowels.

I think it also handles words where the first vowel comes in the end of the word, but the site does not agree.

Any insights are appreciated.

Your code so far


function translatePigLatin(str) {
  console.clear();
var arr = str.split('');
var vowelEx = /a|e|i|o|u|y/i;
var longAdd = ['way'];
var add = ['ay'];

if (vowelEx.test(arr[0])) {

arr.push(...longAdd);
//console.log(arr);
 
}

else if(arr[1] === 'l') {

  arr.push(arr[0] + arr[1]);
  arr.push(...add);
  arr.shift();
  arr.shift();
}


else {

arr.push(arr[0]);
arr.push(...add);
arr.shift();


}
  //console.log(arr);
var fStr = arr.join('');
  console.log(fStr);

  return fStr;
}

translatePigLatin("lgrthma");

Your browser information:

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

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

“y” is not a vowel.

Thank you.

Removing “y” from my vowelEx variable did not change the result.

There must be something else.

Why are you specificallly testing for the letter “l”?

I need to treat “gl” as a consonant cluster in order to pass test 3.

I should make the code able to handle a wider variety of consonant clusters. That is true.

I’m working on that now. I don’t see how it would stop me from passing the final two tests. Although, if I could see why I wasn’t passing, I wouldn’t be on the help forum.

To pass the last two tests you need to handle different words that are not those tested in the other tests

For example your test what result would give with grove?

That makes sense.
Let me see what I can do.

Ok, I changed the “else if” statement. I’m still getting the same result, though.

else if(vowelEx.test(arr[1]) === false) {

  arr.push(arr[0] + arr[1]);
  arr.push(...add);
  arr.shift();
  arr.shift();
}

I’m thinking I need to make my code work with three letter initial consonant clusters as in “throw”.

You need to make it work with any consonant cluster, even with 3,4,5,6 letters or a whole word without vowels

On a side note related to the line above. The above works, but a simpler way is to use a character class/set. For example, if I want to match the letter a, b or c, then I could write:

/[abc]/

Instead of looking for consonant clusters explicitly, maybe you want to look at the index of the first vowel.

1 Like

Ok. So I am trying to use this code:

console.log(str.match(vowelRegEx));

to find the index of the first vowel in the string ‘consonant’.

I was surprised at what I got in the console:

["o", index: 1, input: "consonant", groups: undefined]0: "o"groups: undefinedindex: 1input: "consonant"length: 1__proto__: Array(0)

From the lesson on extracting matches, I had expected this code to just return ‘o’.

Can I use match to get the index of the first vowel or am I barking up the wrong tree?

Oh, I see. It returns an object. So, I can access the index property with object.index.

Ok. I managed to pass this one.

Thanks for your help, everyone.