Pig Latin challenge help

Tell us what’s happening:
I can pass all challenges except words without vowels. I’ve tried adding other if statements in to handle that but still not getting it to pass.

Any sugestions?

Your code so far


function translatePigLatin(str) {
let vowels = str.search(/[aeiou]/g);
let cons = str.search(/a-df-hj-np-tv-z/g);

  if (vowels === 0) {
  return str + 'way';
} else {
  return str.substr(vowels) + str.substr(0, vowels) + 'ay';
} 
}

console.log(translatePigLatin("california"));

Your browser information:

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

Challenge: Pig Latin

Link to the challenge:

The first thing I will mention is that all words have vowels (OK, I’m sure you can find a few unique exceptions, but you get my point), so this statement doesn’t make sense. Remember, you are not looking for words without vowels, you are looking for words that begin with a vowel. The second bullet point in the instructions say just that:

  • If a word begins with a vowel, just add “way” at the end.

So my hint would be to look at your regex at the beginning. What is it currently searching for? How do you need to modify it so that it searches for what I just mentioned?

If you want to add an else-case for words without vowels, you’d have to figure out what your vowels variable is in that case. Right now you’re only checking for the case if vowels === 0.

I’m not saying you’re wrong but I don’t write the FCC curriculum. Here is the error I get:

Should handle words without vowels. translatePigLatin("rhythm") should return "rhythmay".

It literally says it should handle words without vowels.

It says:

  • If a word begins with a consonant, …
  • If a word begins with a vowel …

So ‘rhythm’ would fall under the first bullet point.

Look at those two bullet points, they are both concerned with the first letter of the word only. That should hopefully be enough of a hint on what you should be searching for in your regex.

P.S. I believe that technically ‘y’ would be considered a vowel in rhythm.

I have also tried using an else if with vowels === 1 and vowels !== 0 which I believe are the same but that just messes up my other statements and return incorrect words.

That is incorrect. If that was the case I could have handled this whole thing differently. What the challenge says:

- If a word begins with a consonant, take the first consonant or consonant cluster, move it to the end of the word, and add "ay" to it.

cluster means all consonants before the first vowels. So glove should come out to oveglay.

I believe so too but I can’t add it to my regex or it will mess up words where y is not considered a vowel, alas my predicament on why I can’t figure a way out on how to accomplish this.

My suggestion would be to focus on the second bullet point first:

  • If a word begins with a vowel, just add “way” at the end.

How would you determine this using a regular expression? What would you return if this is true?

It’s not incorrect, read the bullet point very carefully:

  • If a word begins with a consonant, take the first consonant or consonant cluster, move it to the end of the word, and add “ay” to it.

That’s all that matters. What you do if that is the case is a different story, then yes you may be moving more than just the first letter. But the point is that the decision on how you should handle this is based entirely upon the first letter.

And as I mentioned above, I would suggest you handle the first letter being a vowel first since that is the easier one. Then you can concentrate all of your effort on the slightly more difficult process of moving one or more letters from the front to the back.

Ok I got it. I was incorrectly using (vowels === 1) . The search() method returns a -1 if no match is found so I should have had (vowels === -1) when I was attempting to add the extra if statement. @jsdisco kind of led me to check that.

Here is the final code that works

if (vowels === -1) {
    return str + 'ay'
  } else if (vowels === 0) {
    return str + 'way';
  } else {
    return str.substr(vowels) + str.substr(0, vowels) + 'ay';
  } 
}

Thanks for the help.

Win-win, I wasn’t aware of the .search method on strings so thx :love_you_gesture: