• 11th October 2008 - By Bill Nunney
    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 :)

  • One Response to “AS3 Quick Tips: How to check if a number is odd or even”

    • Jason on 11 November, 2008

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

      Thanks a bunch!

    Leave a Reply