4 Replies Latest reply on Oct 7, 2009 2:05 PM by kglad

# I know this can be shortened but i'm stuck trying

Hi all

I'm so sure that this can be shortened but i'm having real difficulties with the for, and if statements, and the orders.. i figure why suffer over it... get some help.

heres the code:

if ((dat) >= "1") { block_1._alpha = 50; }

else { block_1._alpha = 100;}

if ((dat) >= "2") { block_2._alpha = 50; }

else { block_2._alpha = 100;}

if ((dat) >= "3") { block_3._alpha = 50; }

else { block_3._alpha = 100;}

if ((dat) >= "4") { block_4._alpha = 50; }

else { block_4._alpha = 100;}

if ((dat) >= "5") { block_5._alpha = 50; }

else { block_5._alpha = 100;}

if ((dat) >= "6") { block_6._alpha = 50; }

else { block_6._alpha = 100;}

if ((dat) >= "7") { block_7._alpha = 50; }

else { block_7._alpha = 100;}

if ((dat) >= "8") { block_8._alpha = 50; }

else { block_8._alpha = 100;}

if ((dat) >= "9") { block_9._alpha = 50; }

else { block_9._alpha = 100;}

if ((dat) >= "10") { block_10._alpha = 50; }

else { block_10._alpha = 100;}

if ((dat) >= "11") { block_11._alpha = 50; }

else { block_11._alpha = 100;}

if ((dat) >= "12") { block_12._alpha = 50; }

else { block_12._alpha = 100;}

if ((dat) >= "13") { block_13._alpha = 50; }

else { block_13._alpha = 100;}

if ((dat) >= "14") { block_14._alpha = 50; }

else { block_14._alpha = 100;}

if ((dat) >= "15") { block_15._alpha = 50; }

else { block_15._alpha = 100;}

if ((dat) >= "16") { block_16._alpha = 50; }

else { block_16._alpha = 100;}

if ((dat) >= "17") { block_17._alpha = 50; }

else { block_17._alpha = 100;}

if ((dat) >= "18") { block_18._alpha = 50; }

else { block_18._alpha = 100;}

if ((dat) >= "19") { block_19._alpha = 50; }

else { block_19._alpha = 100;}

if ((dat) >= "20") { block_20._alpha = 50; }

else { block_20._alpha = 100;}

if ((dat) >= "21") { block_21._alpha = 50; }

else { block_21._alpha = 100;}

if ((dat) >= "22") { block_22._alpha = 50; }

else { block_22._alpha = 100;}

if ((dat) >= "23") { block_23._alpha = 50; }

else { block_23._alpha = 100;}

if ((dat) >= "24") { block_24._alpha = 50; }

else { block_24._alpha = 100;}

Any help would be awesome.. cheers all

B

• ###### 1. Re: I know this can be shortened but i'm stuck trying

for(var i:Number=1;i<=23;i++){

if(dat>=i.toString()){

this["block_"+i]._alpha=50;

} else {

this["block_"+i]._alpha=100;

}

}

// but i doubt you want to compare strings in that if-statement, so you should probably remove that toString() method.

• ###### 2. Re: I know this can be shortened but i'm stuck trying

I have to question/wonder about your comparing (dat) to a string, which is what I have shown below.  So if you really intend to compare things as numbers you may want to treat them as numbers, as in...  if (Number(dat) >= i)

for(i=1; i<25; i++){
if ((dat) >= String(i)) { this["block_"+i]._alpha = 50; }
else { this["block_"+i]._alpha = 100; }
}

• ###### 3. Re: I know this can be shortened but i'm stuck trying

Perfect

Cheers for that

What gets me is both of those codes worked fine, even though they were written very differently.

The dat' bit is what i had down as a day. Its for a calendar. so dat will (for a christmas calendar) only go up to 24, and will be displayed as a number.