+ 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);
3 Answers
+ 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
+ 21
Rad!! Thank you for your help :)
0
I understand it (3*3=9) but the next one I don't understand Ă +=2 please explain it