#709. Sum of even numbers
When the green flag is clicked, this script runs. What is the value of the **s** variable?
when green flag clicked set [i v] to (0) set [s v] to (0) repeat (20) change [i v] by (1) if <((i) mod (2)) = (0)> then change [s v] by (i) end end
# When the green flag is clicked
i = 0
s = 0
for _ in range(20):
i += 1
if (i % 2) == 0:
s += i
// Scratch's mod gives a positive remainder even for negative numbers
function mod(a, b) {
return ((a % b) + b) % b;
}
let i = 0, s = 0;
// When the green flag is clicked
i = 0;
s = 0;
for (let j = 0; j < 20; j++) {
i += 1;
if ((mod(i, 2)) === 0) {
s += i;
}
}
💡 Not sure? Build it in Scratch and try it out.