Intermediate Algorithm Scripting: Pig Latin Doubt!

The question:

Translate the provided string to pig latin.

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”.

If a word begins with a vowel you just add “way” to the end.

Input strings are guaranteed to be English words in all lowercase.

Answer:


function translatePigLatin(str) {
  if (/^[aeiou]/.test(str)) {
    return str+"way";
  }
  else if (/^[^aeyiuo]+$/.test(str)) {
    return str+"ay";
  }
  else {
    return str.replace(/([^aeiou]+)(\w+)/,"$2$1")+"ay";
  }

  return str;
}

translatePigLatin("consonant");

The website says that it is incorrect because it should handle words without vowels. I don’t understand why…

Thanks in advance.

Check if your algorithm handles the word rhythm. Remember that “y” is not a vowel in the English alphabet.

1 Like

That’s it!

In portuguese y is not a vowel as well… I got [^aeyiuo] in stackoverflow and did not realize that it was wrong!!

Thanks man!

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums