Pig Latin (i am aware this topic is similar to others)

Tell us what’s happening:
I am not sure why this does not work, i used regex to match the vowels, isolated the characters by index position and used an if statement to return the results using the slice method.

Your code so far


function translatePigLatin(str) {
  var firstLetter = str.match(/[aeiou]/);
  var firstIndex = str.indexOf(firstLetter);

  if(firstIndex > 0) {
    return str.slice(firstIndex) + str.slice(0, firstIndex) + "ay";
  }
  return str + "way";
}

translatePigLatin("consonant");

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/

What do the failing tests say?

1 Like

Specifically, it seems to be failing for one word: rhythm. What do you expect your function to return for that?

The response states"Should handle words without vowels."

What your function currently does is add “way” to the end of a word with no vowels (because firstIndex will not be greater than 0). See below:

But if you think about it, a word with no vowels is basically just one long “consonant cluster”, and we know that we are supposed to put the string “ay” after the first consonant cluster.

How can you solve this problem?

1 Like

The function should return hythmray since the word does not begin with a vowel.

I am thinking that i need to amend my if statement to accommodate for such an instance? i am not sure if that is sound but that seems to make sense to me

The word does not contain a vowel. Rethink the return on that one.

Also, it might be useful to see what you’re returning, so you can compare that to what the tests are expecting. I might change your code a little: rather than simply returning the string value, save the string value in either case to a local variable. Then you can console.log that variable before you return it.

Doing this will let you see what your function is actually doing.

Wanna like this post TWICE!

Thank you that makes sense to me, much appreciated.