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: [email protected] [mailto:[email protected]] On Behalf
Of Wally Kolcz
Sent: Friday, April 09, 2010 9:00 PM
To: [email protected]
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
_