For loop with if statement

Joined
Sep 14, 2023
Messages
1
Reaction score
0
Hello everyone;

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

Thank you in advance

JavaScript:
let gannak = 0;
debugger;
for (i = 10; i < 99; i++) {
  if (i % 3 === 0 && i % 5 === 0) {
    gannak++;
  }
}
console.log(gannak);
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
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);
  }
}
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
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:

Ask a Question

Want to reply to this thread or ask your own question?

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top