Thursday, July 14, 2011

Review

Variables:
Containers for data.

Cannot start with a number.
Camel casing.
Cannot be protected words.
Give it a unique name.

Tell it what class of data you want it to store:
Numbers, String, Boolean, Integer, Unsigned Integer, Arrays

var myFirstVariable:Number = 250.5     //any number including decimals 
var myInteger:int = 36;   //any whole number that is positive or negative 
var myUnsignedInteger:uint = 2000;    //any positive whole number  
var myString:String = "Hello  World";    //just plain text
var myBoolean:Boolean = true;  //true or false; 1 or 0;
 
Arrays:
var myFirstArray:Array = new Array();    //creates a new empty array
myFirstArray[0] = "Ross";   
myFirstArray[1] = "Higgins";  
myFirstArray[2] = 36;  

trace(myFirstArray);   // send the values of the array to the output panel

Condensed Way to Make an Array:
var mySecondArray:Array = ["Ross", "Higgins", 36]; 

.push 
mySecondArray.push("Married");  //PUSH adds a value to the end of existing array
.pop 
mySecondArray.pop();   //POP takes the last one off



.splice
public splice(startIndex:Number, [deleteCount:Number], [value:Object]) : Array

mySecondArray.splice(1,0,"California");     //SPLICE adds a value to the array in a specified position
mySecondArray.splice(1,1);    //the position, number of items we are removing


.length
trace("length:" + mySecondArray.length); 

OUT PUT:    
length:3





HELP:
highlight one of the blue words, like Array, and right click then select View Help



Operators:
==   equality operator
>     greater than
<     less than
>=  greater than or equal to
<=  less than or equal to
!     not operator
!=   not equal to
!>  greater than
%  modulus operator (returns the remainder of the divsion of 2 numbers)
  10 % 3 = 1 //10 divide by 3 over and over until it can no longer divide and it gives you the remainder
  25 % 5 = 0  //you can use this to check if your array has even values
&&  and operator
||      operator
++   increment operator
--    decrement operator

Conditional Statements:
//IF STATEMENTS
if ( myInteger == 362) {
    trace("the first statement is true");
} else if (myFirstVariable = 300) {
    trace("the second statement is true");
} else {
    trace("everything else is false");
}

//AND OPERATOR
if (myInteger == 36 && myString == "Hello") {
    trace("both conditions are true");
}
//OR OPERATOR
if (myInteger == 36 || myString == "Hello") {
    trace("at least one of the conditions are true");
}

//SWITCH STATEMENTS
var a:Number = 35;

switch(a) {
    case 0:
        trace("#1");
        break; //if you don't have break then it will continue tracing everything below the match
       
    case "Hello World":
        trace("#2");
        break;
       
    case 35:
        trace("#3");
        break;
   
    default:
        trace("nothing else was a match");
}

Loops:
//FOR LOOPS 
//For loops are finite in their execution
//There are 3 parts in constructing Loops
:
  set up a  variable, condition, incrimentor


for(var i:int = 0; i < 5; i++) {
    trace("Hello");
}

OUT PUT: 
Hello
Hello
Hello
Hello
Hello



//counting up

for(var j:int = 0; j < 10; j++) {
    trace(j);
}

OUT PUT:
0
1
2
3
4
5
6
7
8
9



//counting down
for(var k:int = 4; k > 0; k--) {
    trace(k);
}

OUT PUT:
4
3
2
1



//WHILE LOOPS
//ends when the condition is met (can create an infinite loop)

var theNumber:Number = 0;
while (theNumber < .5) {
    theNumber = Math.random();  //u do this so you don't get infinite loop.
                                // Math.random will pick numbers above and below .5
    trace(theNumber);
}





Random Numbers:
Math.random
Math.random(); //picks a number between 0 - 1; never 0 or 1;

var theRandomNumber:Number = Math.random();
trace(theRandomNumber);

theRandomNumber = Math.random() *5;
trace(theRandomNumber);



rounding

Math.round();   //like elementary school; >= .5 rounds up; < .5 rounds down 
Math.ceil();   //always rounds up 
Math.floor();   //always rounds down
//(you have to pass a number into the parenthesis to make the above work) Math.round(33);

theRandomNumber = Math.ceil(Math.random() * 100); //pick a random number between 1-100



Functions
1.
function showMessage():void {
   
 }

//void is it's data class. Because we are not returning a value out of the function.

2. 
 function showMessage():void {
     trace("Hello");
 }
showMessage();

//call the function 

3. 
function showMessage(theMessage):void {
     trace(theMessage);
 }
showMessage("This is Stuff");
showMessage("Hello World");



//pass the information inside the var theMessage into the function

4.  Converting to Farenheit
function celciusToFarenheit(theCelciusNumber):Number {
    return(9/5) * theCelciusNumber + 32;
}

var theTemperature:Number = celciusToFarenheit(28);
trace(theTemperature);



HOMEWORK:
Read Chapter 1 & 2
 

No comments:

Post a Comment