Problem when verifying if a number is a module of 2 in pure js

5

I'm trying to do this:

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

i is the variable of is coming from a for loop.
Why is not it working?

    
asked by anonymous 05.10.2016 / 16:37

1 answer

7

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

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

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