I was using a nested while loop, and ran into a problem, as the inner loop is only run once. To demonstrate I’ve made a bit of test code:
let i = 1, j = 1;
while (i < 5) {
console.log('outer', i);
while (j < 4) {
console.log('inner', j);
j++;
}
i++;
}
Help me how to solve this problem, I have tried in every but the reuslt was the same
5
Answers
Your problem is that you never reset
j
to 1, after running the inner loop.this will reset j to 1 after running the inner loop, and the while loop will now run every time the outer loop runs.
Just move the
j
variable inside the first while loopYour not reseting the inner counter var.
Could you please put the "j" inside the first loop and try