+ 4

Why 0?

var x=1; for (var i = 0; i < 3; i++) { x*=i; } document.write(x)

1st May 2017, 7:58 PM
Kristina Hakobyan
Kristina Hakobyan - avatar
7 Respostas
+ 5
i = 0: x = 1 * 0; i = 1: x = 0 * 1; i = 2: x = 0 * 2; i = 3: loop terminates remember anything times 0 is 0
1st May 2017, 8:01 PM
Edward
+ 10
1st iteration of the loop i is 0, so x*=0 -> x is 0. 2nd iteration: i is 1, x*=1 -> x is 0, because 0*1=0
1st May 2017, 8:03 PM
Tashi N
Tashi N - avatar
+ 3
ok thx
1st May 2017, 8:02 PM
Kristina Hakobyan
Kristina Hakobyan - avatar
+ 2
I can think when after for I need last number here it's 3 because after 3 loop break
1st May 2017, 8:04 PM
Kristina Hakobyan
Kristina Hakobyan - avatar
+ 2
but here x*=i it's x =x *I but my x =1 not 0
1st May 2017, 8:31 PM
Kristina Hakobyan
Kristina Hakobyan - avatar
+ 1
yea but the first i in the for loop is 0 so x = x * i x = 1 * 0
1st May 2017, 8:32 PM
Edward
+ 1
oh good it's look into loop
1st May 2017, 8:43 PM
Kristina Hakobyan
Kristina Hakobyan - avatar