Combinations of for and while loop


#1

for (var x = 05; x<101; x=x+5){
console.log(x);
}

while (x===50) {
console.log("10 above and 10 below")
}

Why dont i get an output like this?

5
10
15
20
25
30
35
40
45
50
10 above and 10 below
55
60
65
70
75
80
85
90
95
100

what's the error in the code? it keeps crashing


#2

You need to put the while loop inside of the for-loop, right now you closed the for-loop too early.


#4

Something like this.. ?
for (var x = 05; x<101; x=x+5){
while (x===50) {
console.log("10 above and 10 below")
x===55
}
console.log(x);
}

P.S. it still crashes :confused:


#5

The cause of the crash. But I suspect you know that now.

while (x===50) {
    console.log("10 above and 10 below");
    x = 55;
}

Still, not not the best example of a loop when an if statement would accomplish the same..

Let's see if we can sum up what I believe you are seeking, as an example. I'll not try to replicate what you have, but rather use one loop to count by tens, and an inner loop to count off the ones.

for ( i = 0; i < 100; i += 10 ) {
    k = 0;
    while ( k++ < 10 ) {
        console.log( i + k );
    }
}

#6