> that solution (the age switch) is what i proposed before, in the first reply

Oops, sorry! I need to pay more attention to the whole thread before I answer.

- Gordon

From: flexcoders@yahoogroups.com [mailto:flexcod...@yahoogroups.com] On Behalf 
Of Joe Cabezas
Sent: Friday, April 16, 2010 11:08 PM
To: flexcoders@yahoogroups.com
Subject: Re: [flexcoders] Re: Can someone rewrite this...



Gordon:

that solution (the age switch) is what i proposed before, in the fisrt reply, :D

PD: Adobe Flex SDK Team?, wow!, nice work adobe! :D

2010/4/16 Gordon Smith <gosm...@adobe.com<mailto:gosm...@adobe.com>>

If age were an int rather than a Number (which it probably should be) then you 
could use a switch:

switch (age)
{
case 0:
case 1:
case 2:
    return 1;
case 3:
case 4:
case 5:
    return 2;
case 6:
case 7:
case 8:
    return 3;
case 9:
case 10:
case 11:
case 12:
    return 4;
case 13:
case 14:
case 15:
    return 5;
case 16:
case 17:
case 18:
    return 6;
default:
    return 0;
}

You could also use an array specifying the last age in each group - [ 2, 5, 8, 
12, 15, 18 ] - and iterate over it until you find the first one where the age 
is <= the array entry. This would involve less code, but it would probably 
execute slightly slower.

Finally, if the age groups were more sensible so that each one contained 3 
ages, then you could just do something like int(age/3) + 1.

Gordon Smith
Adobe Flex SDK Team


From: flexcoders@yahoogroups.com<mailto:flexcoders@yahoogroups.com> 
[mailto:flexcoders@yahoogroups.com<mailto:flexcoders@yahoogroups.com>] On 
Behalf Of Wally Kolcz
Sent: Friday, April 09, 2010 9:00 PM
To: flexcoders@yahoogroups.com<mailto:flexcoders@yahoogroups.com>
Subject: Re: [flexcoders] Re: Can someone rewrite this...



Thanks for the catch on the GT. Haven't tried to run it yet. Just wrote is and 
hoped there would be a switch/case that is easier than having to case out each 
age..

On 4/9/2010 11:43 PM, Joe wrote:


it curious but a person who are 2000 years old, with your implementation it's 
group 1

run this code:

package
{
import flash.display.Sprite;

public class test extends Sprite
{
public function test()
{
trace(this.getAgeGroup(2000));
}

private function getAgeGroup(age:Number):int {
if (age >= 0 && age >3){ //Birth to 2
return 1;
}else if (age >=3 && age >6){ //3 to 5
return 2;
}else if (age >=6 && age >9){ //6 to 8
return 3;
}else if (age >=9 && age >13){ //9 to 12
return 4;
}else if (age >=13 && age >16){ //13 to 15
return 5;
}else if (age >=16 && age >19){ //16 to 18
return 6;
}else { //Older or Adults
return 0;
}

}
}
}

maybe you want to do this:

private function getAgeGroup(birthday:Date):int {

var age:int = getAge(birthday);

if (age>=0&&age>3) {//Birth to 2
return 1;
} else if (age >=3 && age <6) {//3 to 5
return 2;
} else if (age >=6 && age <9) {//6 to 8
return 3;
} else if (age >=9 && age <13) {//9 to 12
return 4;
} else if (age >=13 && age <16) {//13 to 15
return 5;
} else if (age >=16 && age <19) {//16 to 18
return 6;
} else {//Older or Adults
return 0;
}

}

see i changed yours ">" with "<" ???

HOWEVER, a switch implementation is like this:

private function getAgeGroup(birthday:Date):int {

var age:int=getAge(birthday);

switch (age) {
case 0 :
case 1 :
case 2 :
return 1;
break;
case 3 :
case 4 :
case 5 :
return 2;
break;
case 6 :
case 7 :
case 8 :
return 3;
break;
case 9 :
case 10 :
case 11 :
case 12 :
return 4;
break;
case 13 :
case 14 :
case 15 :
return 5;
break;
case 16 :
case 17 :
case 18 :
return 6;
break;
default :
return 0;
break;
}

}

Point for testing!!!!! :D

greetings from Chile!, Latin America
_



Reply via email to