The Advanced Solution of "Pig Latin" can cause a endless loop

The Advanced Solution of "Pig Latin" can cause a endless loop


Tell us what’s happening:

The Advanced Solution of “Pig Latin” can cause a endless loop
if you input a word without vowels,the while will not stop logically,although it actually stops.

Your code so far

// function translatePigLatin(str) {
//   if(/[aeiou]/.test(str[0])){
//     return str= str +"way"
//   }else if(!/[aeiou]/g.test(str)){
//     return str= str +"way"
//   }else{
//   return str.replace(/(\w*?)(?=[aeiou])(.*)/,"$2$1ay");
//   }
// }

function translatePigLatin(str) {
    var strArr = [];
    var tmpChar;

    function isConsonant(char) {
        return !/[aeiou]/.test(char);

    if (!isConsonant(str.charAt(0)))
        return str + "way";
        strArr = str.split("");

    while (isConsonant(strArr[0])) {
        tmpChar = strArr.shift();

 return strArr.join("")+"ay";

// test here

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6726.400 QQBrowser/10.2.2265.400.

Link to the challenge:


Your while loop condition is always met, because the first character is a consonant. Why? Because you are only pushing consonants back on to the end of strArr when the entire string is made of consonants.