Posted by: pixelero | April 28, 2008

[literal Array][index];

Some simple actionscript hint for arrays:

not only can you define a new array by just writing:
var items:Array = [“first”, “second”, “third”]; // = new Array(“first”,”second”,”third”);

but you can also access items on a literal array by using an index [1,2,3] [i] ; !
If you have a limited number of cases you can replace a function, switch or ifs with this.

for (var i:int=0; i<3; i++) {
choices[i].name = [“D2”, “D3”, “D4”] [i] ;
// choices[0].name= “D2” … etc

var dimVal:int= [ 0, 1, 4, 13, 40 ] [n] ; // var n:int, precounted values for function (3^n -1)/2

var someColor:uint = [0xFF0000, 0xFF00, 0xFF, 0xFFFF00, 0xFF00FF, 0xFFFF] [n] ;

works in javascript as well:

var someFib = [1, 1, 2, 3, 5, 8, 13][4];
alert(“some Fibonacci number: “+someFib);

If I was a teacher, I’d now give you a task to write a script converting an integer, 0<i<5000, to a Roman Number returned as a string …

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: