UK, July 3, 2009, 5:22 am
Login | Register

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

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 :)

VN:F [1.1.7_509]
Rating: 8.5/10 (2 votes cast)
Share and Enjoy with your favourite Bookmarking:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

Related Wuup Stuff:



You're Thoughts & Discussion
One comment for “AS3 Quick Tips: How to check if a number is odd or even”
  1. Great tip - exactly what I was in need of for a project.

    Thanks a bunch!

    VA:F [1.1.7_509]
    Rating: 5.0/5 (1 vote cast)

    Posted by Jason | November 11, 2008, 4:46 am

Post you're thoughts...