News for February 2010

Actionscript Modulo

I recently began taking an Action script 3 class at NYU for continuing education.
During my first class we covered the basics of how to create a class, extend a class create a constructor as well as functions. In addition we reviewed basic conditional statements and loops. My teacher assigned us some home work which dealt with a classic question that involves Modulo’s in ActionScript 3.

here it is:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five, print “FizzBuzz”.

My solution:

package {
       //Here we import the Sprite class
	import flash.display.Sprite;
        //Here we extend the Sprite class
	public class MyClass extends Sprite {
		public function MyClass() {
                        //Instantiate the for loop
			for (var i:uint = 0; i < 100; i ++) 
			        //Declare our variables as Modulos
				var Fizz:Number = i%3;
				var Buzz:Number = i%5;
			        //First statement states, if Fizz and Buzz 
			        //are both divisible by both 3 and 5 trace 
			        //this: FizzBuzz				
				if (Fizz ==0 && Buzz == 0) 
					trace("FizzBuzz :   " + i);
			        //Second statement states, if number is 
			        //divisible by 3 trace "Fizz"
				else if (Fizz == 0) 
					trace("Fizz :   " +i);
			        //Third statement states, if number is
			        //divisible by 5 trace "Buzz"
				else if (Buzz==0) 
					trace("Buzz:   "+i);
			        //Fourth Statement states, if number is
			        //not divisible by 3 or 5 trace "i"
					trace( i );

Posted: February 6th, 2010
Categories: code, tutorial
Comments: No Comments.