# Inside the if element

Write a program that prints the sum of all odd numbers from 1 to 50 using a `for` loop.

``````let sum = 0;

for (let num = 1; num <=50; num++) {
if (num % 2 !== 0) {
sum += num;
}
}
console.log(sum);
``````

The “if” elements confuses me. What do they mean? num % 2 !==0.

Hi there,

The ‘%’ represents the ‘modulus operator’.

This refers to the remainder left over when a number is divided by another number.

Example 4 % 2 = 0, because when you divide 4 by 2, 2 divides perfectly into 4, leaving no remainder.

In the case of 5 % 2, the answer would be 1, since when you divide 5 by 2, the answer is 2, but with 1 left over - this left over part if the remainder and this equates to the modulus.

This is useful when determining whether a number is odd or even, as the modulus of an even number will always give ‘0’ when divided by 2, but the modulus of an odd number will always give 1 (this would work differently for values such as 0 and 1 obviously…).

In your example !== means ‘no equal to’, so maybe you can now figure out what the code means?

Hope this helps.

They are using the remainder operator

when you divide two numbers there will be a remainder

for example 12 /2 is 6
but it has a remainder of 0 because we are dividing two even numbers

but if you have 13/6 you would get 2.16 with a remainder of 1

this expression is used to check if a number is odd

if num is a even number, then the result would be false

for example

12 % 2 !== 0

if num is odd, then result would be true and `sum += num` would run

for example

11 % 2 !== 0

hope that helps

So, divided by 2 but not equal to “0” the modulus of an even number, so it’ll be an odd number.? Yes?

Exactly. If, (after being divided by 2), the remainder is 1, it means that the number is odd.

In other words, the condition is checking whether the number is odd or not.

1 Like