bobtta bobtta - 19 days ago 6
Javascript Question

While loop inside while loop [Javacript]

how to use while loop inside while loop without executing the next loop (from outside while loop) unless the inner while loop done executing?

var x = 0;
var y = 0;
while(x < 10){
while(y < 10){
console.log(y);
y++;
}
x++;
}

Answer

I'm guessing your problem here is that at the end of the inner loop, y is set to 10 and it never is reset to 0. Try this instead:

var x = 0;
var y = 0;
while(x < 10){
   while(y < 10){
      console.log(y);
      y++;
   }
  y = 0;
  x++;
}

Otherwise, once the inner loop finishes once, it never runs again.