AS3 Quick Tips: How to check if a number is odd or even

Flash CS3 Professional

At some point in a developers life you find the need to check if a number is odd or even. Whether it be to make a staggered output (alternating row colours) or a more complicated calculation. For this purpose there is a special operator called “Modulo” (admit it,  it sounds cool).

Modulo is represented by the percent symbol “%”. Modulo’s behavior is to return the remainder of the left expression divided by the right expression.

Example:

var myModulo:Number = 3 % 2;
trace(myModulo);

The above example would output the remainder of the calculation 3 / 2 which is ‘1′.

In the following example i create colour alternating lines using a for loop and Modulo in an if statement:

Any questions or requests please leave a comment :)

2 Comments

  1. Great tip – exactly what I was in need of for a project.

    Thanks a bunch!

  2. I keep coming back to this post ever 6 months, forgetting how to do this, thanks for this!

Leave a Reply