Why is 0 the value of the code below when "sum+=i" means that the iterations of the loop are to be added? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Why is 0 the value of the code below when "sum+=i" means that the iterations of the loop are to be added?

var sum=0; for(var i=1;i<=10;i++) { if(i%5===0 && i%3===0) { sum+=i } continue; } document.write(sum);

27th Jun 2019, 1:33 PM
eMBee
eMBee - avatar
6 Answers
+ 6
The loop goes all the way up to ten. The first number evenly divisble by both 5, and 3 is 15.
27th Jun 2019, 1:36 PM
Jax
Jax - avatar
+ 6
@Airree oh 🤔 so do you mean that if the if statement contains (i%4===0 && i%3 === 0), we would still have 0 as the output?
27th Jun 2019, 2:00 PM
eMBee
eMBee - avatar
+ 6
Mofey i%anynumber === 0 is going yo be true, only if i is divisble by the number evenly. Therefore, if you have two statements like this, it will have to be evenly divisible by both numbers.
27th Jun 2019, 2:02 PM
Jax
Jax - avatar
+ 6
Alright Jax, thanks for the explanation
27th Jun 2019, 2:04 PM
eMBee
eMBee - avatar
+ 5
Jax so do you mean that the output is zero because there's no number divisible by 5 and 3 below the numbers of 10?
27th Jun 2019, 1:39 PM
eMBee
eMBee - avatar
+ 3
Yes, the first number divisible by 3 and 5 is 15.
27th Jun 2019, 1:50 PM
Airree
Airree - avatar