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);
6 Answers
+ 6
The loop goes all the way up to ten. The first number evenly divisble by both 5, and 3 is 15.
+ 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?
+ 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.
+ 6
Alright Jax, thanks for the explanation
+ 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?
+ 3
Yes, the first number divisible by 3 and 5 is 15.