Should handle words without vowels: Pig Latin

Should handle words without vowels: Pig Latin
0.0 0

#1

Tell us what’s happening:

Hello guys, my code for this exercise mostly works (despite being a bit hard to read) but it isn’t passing the last test regarding words without vowels. I ran it in repl.it and it came out with an output of ‘cnpltay’. Is there anything I’m doing incorrectly? Thank!

Your code so far


function translatePigLatin(str) {
  if (str.match(/[aeiouy*]/)=='') return str + "ay";
  if (str.match(/^[aeiouy]/)) str += "way";
  else str = str + str.match(/^\w+?(?=[aeiouy])/) + "ay"; 
  while (str.match(/^[aeiouy]/)==null) {
  str = str.split("").splice(1).join("");
  }

  return str;
}

translatePigLatin("cmnplt");

Your browser information:

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

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


#2

When you call your function with “cmnplt”, the else code block below executes and str gets assigned the value “cmnpltnullay”.

Why? Because before the else code, str was equal to “cmnplt” and then you concatenate the value returned from the match which is null. When everything attempt to concatenate a string with something else, JavaScript will coerce the final value into the combination of two strings.

Then your while loop causes the final value of str to be “ullay” instead of the correct value of “cmnpltay”.


#3

Hello Randall,

Thank for the help!
I made the first if statement so that if the input was purely consonants then it would go through that first, but I did not think that it would have to be set equal to null and ‘’. Thanks for helping me find where my error was.