While conditions not working as intended

Tell us what’s happening:
While condition not working as intented, I expect the loop too stop as soon as it finds a vowel, but it does one more cycle before it stops for “consonant” input, and for “glove” it stops right after first consonant

translatePigLatin("consonant"); //output nsonantcoay where it should be onsonantcay 
translatePigLatin("glove"); //output lovegay where it should be oveglay
  **Your code so far**

function translatePigLatin(str) {
str = str.split('');
if(str[0] == 'a' || str[0] == 'e' || str[0] == 'i' || str[0] == 'o' || str[0] == 'u')
{
  str.push('way')
}
else
{
  let i = 0;
  //Here I think is broken!
  while(str[i] != 'a' && str[i] != 'e' && str[i] != 'i' && str[i] != 'o' && str[i] != 'u')
  {
    let c = str.shift();
    str.push(c);
    i++;
  }
  str.push('ay');

}
console.log(str.join(''));
}

translatePigLatin("consonant");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0

Challenge: Pig Latin

Link to the challenge:

This is not a condition for a loop;

while(str[i] != 'a' && str[i] != 'e' && str[i] != 'i' && str[i] != 'o' && str[i] != 'u')
let i = 0;
while(i<str.length){
statement.....
i++;
}

Well, a while loop is different. It will loop so long as the condition matches. Another way to write the same thing:

while( !"aeiou".includes( str[i].toLowerCase() ) ){
  //...
}

This simply says “while str[i] is not a vowel…” But the problem is, in order to check it, i has already been incremented which may throw things off some.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.