Pig Latin - How does the while loop end?

Tell us what’s happening:

Hello,

Can someone Kindly explain how does this while loop end if there are only consonants and no vowel in the argument (arr)?
As far as i understand,
The loop starts at charAt(0) —> shift() it —> push() it into the SAME array (strArr).
How does the loop end if there are no vowels in the word and the challenge needs me to check for that condition also.

Thank you.

Loving freeCodeCamp <3

Your code so far


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

    // check if the char is consonant using RegEx
    function isConsonant(char) {
        return !/[aeiou]/.test(char);
    }

    // return initial str + "way" if it starts with vowel
    // if not - convert str to array
    if (!isConsonant(str.charAt(0)))
        return str + "way";
    else
        strArr = str.split("");

    // push all consonats to the end of the array
    while (isConsonant(strArr[0])) {
        tmpChar = strArr.shift();
        strArr.push(tmpChar);
    }
 // convert array to string and concatenate "ay" at the end  
 return strArr.join("")+"ay";
}

// test here
translatePigLatin("consonant");

Your browser information:

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

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

this is checking if the first letter of the array is a consonant, if it is then it is removed from be beginning of the array and added at the end , and then the new first letter is tested, and keeps going till the first letter is a vowel

if there are no vowels it is an infinite loop

How should i modify the code then?
Since the challenge requires the code MUST BE ABLE TO HANDLE WORDS WITHOUT VOWELS

check if there is any vowel before the loop then

the simplest way you could use is a regex

1 Like

Awesome.
Thank You so much @ieahleen.