How to do correct while loop in JS? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 ++; }

5th May 2023, 7:35 PM
Nadia Den
Nadia Den - avatar
2 Answers
+ 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++; }
5th May 2023, 8:33 PM
JaScript
JaScript - avatar
+ 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.
5th May 2023, 7:49 PM
Etienne Hosman
Etienne Hosman - avatar