Determine if given number is even without using a mathematic operator


I don’t fully understand how thiis works. Can someone explain it more detailed?

Let’s play this with the argument 3:
isEven(3);
When called, a for loop starts at 1 and finishes at 3.
The variable isEven is initialized as true.
So when i===1 in the first iteration, the variable is turned to its opposite – false. That’s what you can do with booleans:
!true === false
!false === true
So when i===1 –> isEven===false.
When i is 2, isEven is true and when i is 3, isEven is false. And that’s what’s returned.

1 Like

Thanks! Made it clear!

1 Like