AS3: Odd or Even


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function isEven(num)
  2. {
  3. // if(num % 2 == 0){return true;}else{return false;} //<–old
  4. return !(num%2);//shorter
  5. // return !(num & 1);//seems the fastest one
  6. }
  7. //
  8. trace(isEven(2));//–> true
  9. trace(isEven(3));//–> false
  10.  
  11. /////////////////
  12.  
  13. function isEven2(num : Number)
  14. {
  15. return num % 2 == 0;
  16. }
  17.  
  18. function isEven3(num : Number)
  19. {
  20. return num & 1 == 0;
  21. }
  22.  
  23. trace( isEven2( 5 ) );
  24. trace( isEven2( 5 ) );

URL: http://www.bit-101.com/blog/?p=729

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.