Javascirpt Pig Latin Challenge

Tell us what’s happening:

Hi everyone,

I’ve been struggling to complete the Pig Latin challenge. My code below seems to do everything it should - I have tested it against all kinds of inputs and compared with one of the examples and they give identical results, but I do keep failing the test “Should handle words without vowels”. Could someone please take a look and see if anything is wrong with my code?

Thanks!

Your code so far


function translatePigLatin(str) {
  let consonantRegex = /^[bcdfghjklmnpqrstvwxz]+/
  let arr = str.match(consonantRegex);
  
  if (consonantRegex.test(str) === false) {
    return str + "way"
  }

  while (consonantRegex.test(str)) {
    str = str.substr(1);
    console.log(str)
  }
 
  return str + arr[0] + "ay";
}

translatePigLatin("eight");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0.

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

Hi Randell,

Thanks for the incredibly fast response! Ideed my code returns “ythmrhay” but after re-reading the instructions this seems correct: “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”.” Also the Wikipedia article linked in the challenge gives the following example: “trash” = “ashtray”

not “trashay”.

Do I understand the challenge incorrectly?

Thanks again.

Got it now! I did not realize “y” is a consonant (in my native language, Czech, “y” is a vowel), therefore I treated “y” as a vowel. After adding “y” to my consonantRegex, I do get the correct return and pass the test.

Many thanks for your help.