+ 1

Can't understand why 4?

var a=[9,5,3]; for (var i = 0; i <3; i++) { a[0]%=a[i]; a[0]+=a[2]; } document.write(a[0])

7th May 2017, 7:05 PM
Kristina Hakobyan
Kristina Hakobyan - avatar
4 Answers
+ 13
The output is 3: 1. iteration, i = 0: 9%9=0 0+3=3=a[0] 2. iteration, i=1 3%5=3 a[0]=3+3=6 3. iterarion, i=2 6%3=0 a[0]=0+3=3 -> output 3.
7th May 2017, 7:16 PM
Tashi N
Tashi N - avatar
+ 12
@shaldem This would explain the output 4 ^^
7th May 2017, 7:33 PM
Tashi N
Tashi N - avatar
+ 6
Hmm... To me it looks more like 3. It goes through the iteration and assigns stuff to a[0]. 1. a[0] is 9, initially 2. a[0] = a[0] % a[0] = 0 3. a[0] = a[0] + 3 = 3 4. a[0] = a[0] % 5 = 3 5. a[0] = a[0] + 3 = 6 6. a[0] = a[0] % 3 = 0 7. a[0] = a[0] + 3 = 3 3
7th May 2017, 7:18 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 6
As I remember in challange question was other cicle, there was for(var i=1;i<3;i++)?
7th May 2017, 7:29 PM
shaldem
shaldem - avatar