+ 22

Help me understand this JS question?

Any JS ninjas willing to volunteer some effort ... What's the story??? var x=1; for(;x<6;x+=2){ x=x*x; } alert(x);

20th Jan 2018, 10:35 PM
JMQ
JMQ - avatar
3 ответов
+ 19
are you already familiar with for loops? in this particular example you skip the first "parameter" of an for-loop which is the initialization of a (for e.g) iterator variable and use the already existing variable named x instead per iteration you increase x by 2 and multiply it with itself *debugger* x is 1 is x smaller than 6? yes => x=x*x (1*1 = 1) x += 2 x is 3 is x smaller than 6? yes => x=x*x (3*3 = 9) x += 2 x is now 11 is x smaller than 6? no! alert (x) 11
20th Jan 2018, 10:43 PM
Kamil
Kamil - avatar
+ 21
Rad!! Thank you for your help :)
20th Jan 2018, 10:45 PM
JMQ
JMQ - avatar
0
I understand it (3*3=9) but the next one I don't understand × +=2 please explain it
21st Jan 2018, 3:53 PM
꧁ Hisbook ꧂
꧁ Hisbook ꧂ - avatar