Tell us what’s happening:
My for loop executes one more time after my expected break. Could you please explain why it does?
Your code so far
function translatePigLatin(str) {
const consonants = ["b", "B", "c", "C", "f", "F", "g", "G", "h", "H", "j", "J", "k", "K", "l", "L", "m", "M", "n", "N", "p", "P", "r","R", "s", "S", "t", "T","v", "V", "y", "Y", "z", "Z", "x", "X", "q", "Q"];
const vowels = ["a", "A", "e", "E", "i", "I", "o", "O", "u", "U"];
let strArr = str.split("")
if(consonants.includes(strArr[0])) {
for(var i = 0; i < str.length; i++){
if(vowels.includes(strArr[i])){
break;
}
strArr.push(strArr[i]);
strArr.push("ay");
strArr.shift();
}
}else{
strArr.push("way")
}
return strArr.join('');
}
console.log(translatePigLatin("consonant"));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36
.
Challenge: Pig Latin
Link to the challenge: