Test if variable is odd or even

Hi Forum

I need to check if a variable is an odd or an even number, but I am pretty stumped. I know you can do this in C#, but I haven’t been able to find anything about Squirrel.

if (IsEven(Counter)) doesn’t work.

Can someone give me a pointer in the right direction?

Thanks in advance

Mike :o)

You want to use the modulo operator!

The modulo operator essentially gives you the remainder of a division… so:

1%2 = 1
2%2 = 0
3%2 = 1
4%2 = 0

`function isEven(num) {
return (num%2 == 0);
}

//…

if(isEven(Counter)) {
//even code
}`

@beardedinventor

Thanks for the swift reply. I think I just had a brain aneurysm, trying to understand it, but that is not your fault. I’m just really not that skilled in this.

I’ll try and figure it out, from your example.

Mike :o)

% is modulus; essentially, what’s “left over” after an integer division.

So eg 5/2 = 2 with remainder 1: 5%2 will equal 1.