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.