+ 2

jAVASCRIPT CHALLENGE QUESTION, CAN NOT FIGURE OUT THE OUTPUT

hI EXPETTS COULD ANYONE EXPLAIN WHY FOLLOW OUTPUT IS 4? APPRCIATE var a = [9,5,3]; for(var i=1;i<3;i++){ a[0]%=a[i]; a[0]+=a[2]; } document.write(a[0]);

26th Jan 2018, 7:18 AM
tess hsu
tess hsu - avatar
3 Answers
+ 6
a[0]=9 now joining for loop. a[0]=9%a[1]=4 then a[0]=a[0]+a[2] ..current value of a[0]=4 so a[0] becomes 4+3=7. agian i value increase to 2 .since 2 is less than 3.it enters for loop.so a[0]=7%a[2]=1 again a[0]=a[0]+a[2]=1+3=4 ..again i increase to 3.but the condition becomes false so it doesn't go inside the for loop so it exits the loop printing current value which is 4.Sorry for bad English.but I hope you get general idea.
26th Jan 2018, 7:29 AM
Sunil Thakali
Sunil Thakali - avatar
+ 3
i don't know if i'm wrong but i think answer should be 4 a = [9,5,3] var i= 1 a[0] = 9%5= 4 a[0] = 4+3 = 7 var i = 2 a[0] = 7%3 = 1 a[0] = 1+3 = 4 so a[0] should be 4
26th Jan 2018, 7:31 AM
‎ ‏‏‎Anonymous Guy
+ 1
thanks so much, it's very clear now
27th Jan 2018, 12:35 PM
tess hsu
tess hsu - avatar