Problem when checking if a number is pure js module 2

Asked

Viewed 37 times

5

I’m trying to do it like this:

if(i % 2){
   console.log(i);
 }

i is the variable is coming from a for loop.
Why isn’t it working?

1 answer

7


You have to compare it with 0 to check if the rest of the division is zero, otherwise you are right:

if(i % 2 == 0){ console.log(i); }

for (let i = 0; i < 10; i++) {
    console.log(i, i % 2 == 0 ? 'é par' : 'é ímpar')
}

  • 1

    Thanks, it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.