+ 2
How to do correct while loop in JS?
let a = -166; let i = 0; while (a > -100 && a < 100) { a = 2*a + 200; console.log(a); i ++; }
2 Réponses
+ 4
// There are many ways for it. The question is what is the task, you tried?
// You could try similar to this:
let a = -166;
let i = 0;
while (i < 10) {
a = 2*a + 200;
console.log(a);
i++;
}
+ 1
The reason why nothing gets logged is because you define a as -166
In the while loop you have the condition that a > -100,
-166 is not bigger then - 100, it is smaller, hence the code doens't enter the while loop.
Hope this helps.