Javascirpt Pig Latin Challenge

Javascirpt Pig Latin Challenge
0

#1

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/


#2

If I call your function like:

translatePigLatin("rhythm");

it returns “ythmrhay”, but it should return “rhythmay”


#3

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.


#4

You understand the challenge correctly, but y is a consonant, along with all the other consonants (the whole word is a consonant cluster) gets moved to the end of the word and gets “ay” added to it. Imagine cutting out all the letters in “rhythm”, so that all you had left was a blank string “” and then moving those letters to the end of the blank string and then adding “ay” on the end. It will appear has if nothing moved, but really the letters are moving.


#5

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.