Home
> InDesign Scripts > QuickTip: Check if a number is even or odd
QuickTip: Check if a number is even or odd
How to check if a number is even or odd? We can use Modulus operator (%), which returns the remainder of a division operation. If you divide some number by two, a remainder of 0 indicates an even number, while a remainder of 1 indicates an odd number. Best and easiest way is to create ‘Number’ prototype.
Here is prototype:
Number.prototype.isEven = function (){ return (this%2 == 0) ? true : false; }
Now declare some number:
var myNum = Number(15);
And check if it’s even or odd:
alert(myNum.isEven()); // returns false
[UPDATE]
Thanks Jongware!
Also, you can do it like this:
alert(!(myNum & 1)); // returns false
That’s it!
Have fun! 😀
Categories: InDesign Scripts
Javascript
It’s also possible to do
alert (!(myNum & 1)) …
Thanks Jongware!
I’ll update the post 😉