For loop with if statement

Boghos

Hello everyone;

The if statement is not clear (i % 3 === 0 && i % 5 === 0) how the calculation happening anyone can explain.

JavaScript:
``````let gannak = 0;
debugger;
for (i = 10; i < 99; i++) {
if (i % 3 === 0 && i % 5 === 0) {
gannak++;
}
}
console.log(gannak);``````

menator01

Looks like any number divided by 3 and 5 that equals 0 advances the counter

VBService

So, i % 3 === 0 checks if i is divisible by 3 without any remainder, and
i % 5 === 0 checks if i is divisible by 5 without any remainder.

The && operator combines these two conditions, meaning both conditions must be true for the if statement to be satisfied.

JavaScript:
``````let gannak = 0;

for (let i = 10; i < 99; i++) {
if (i % 3 === 0 && i % 5 === 0) {
//gannak++;
console.log(i, ++gannak);
}
}``````

VBService

any number divided by 3 and 5 that equals 0
that's not exactly true (this description is too simplified, no offense )

Division sign looks like "/" i / 3 === 0, i / 5 === 0, but sing "%" mean modulo operator.
The modulo operator calculates the remainder when one number is divided by another.

The difference between division "/" and modulo "%" operators can be summarized as follows:

• The "/" operator performs division between two numbers and returns the result as a floating-point number (float). In division, the result may include a decimal part if necessary to accurately represent the division result. For example:
JavaScript:
``let result = 10 / 3; // The result will be approximately 3.33333...``
• The "%" operator performs the modulo operation, which returns the remainder when one number is divided by another. In this case, the remainder of dividing 10 by 3 is 1 because there is 1 left over after the division. For example:
JavaScript:
``let remainder = 10 % 3; // The remainder will be 1``
Both operations have their uses in different programming situations.
A very popular example of modulo operation, find even numbers in range 50 to 100
JavaScript:
``````for (let i=50; i<=100; i++)
if (i % 2 === 0)
console.log(i);``````

JavaScript:
``````// Create a new array 'arr' with a length of 10, filled with zeros, and then map over it
// to replace each element with a random integer between 10 (inclusive) and 100 (exclusive).
const arr = new Array(10).fill(0).map(() => Math.floor(Math.random() * (100 - 10)) + 10);

// Output the original 'arr' to the console.
console.log(arr);

// Create a new array 'evenarr' by filtering 'arr' to only include elements that are even (modulo by 2).
const evenarr = arr.filter(i => i % 2 === 0);

// Output both the original 'arr' and the 'evenarr' to the console.
console.log(arr, evenarr);``````

Last edited:

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Members online

No members online now.