For loop executes one more time after my expected break

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:

Whenever I am in a similar situation and don’t know why it’s not working, most of the time it will help to try and walk through the logic with an example.

So let’s take “consonant” as the input for your function.

You create the array [‘c’, ‘o’, ‘n’, ‘s’, ‘o’,… and so on]

The first letter is a consonant so we go to the for loop.
The first letter is not a vowel, so we go to the else statement.
We now add [‘c’, ‘ay’] to the end of the array
and we remove the first element.

We go through the for loop again

The second element is not a vowel, so we go to the else statement again…

This is where the issue is :wink: since you removed the first element from the array, ‘o’ is now the first element, ‘n’ is the second, ‘s’ is the third and so on.

Note: I did this by just thinking through it, but of course you always have ‘console.log()’ as a tool that you can use to check what is going on at each step in your code.

So in this case, you could add a “console.log(strArr) and a console.log(i)” at the top of the if statement in the for loop. That way you can easily see, at what position the condition is met. If this position doesn’t make sense, you can take a look at the array and see why it actually does.

Hope this helped.

1 Like

The second element is not a vowel, so we go to the else statement again…

thanks you very much you made my day!

i updated code like this and it works now. thanks you again. ==>

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”, “w”, “W”];
const vowels = [“a”, “A”, “e”, “E”, “i”, “I”, “o”, “O”, “u”, “U”];
let strArr = str.split("")
let i = 0;
if(consonants.includes(strArr[0])){
while(consonants.includes(strArr[0]) && i < strArr.length){
strArr.push(strArr[0]);
strArr.shift();
i++;
}
strArr.push(“ay”)
}else{
strArr.push(“way”)
}
return strArr.join("");
}
console.log(translatePigLatin(“rhythm”));