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: