FWIW, if you are worried about performance, I would test the efficiency of 
using a map, especially if age is an int and not a Number.  Even if it is a 
Number I might still test out a map with a Math.floor on the age.

Var agemap:Object {
  0 : 1, 1: 1, 2: 1, 3: 2, 4: 2, 5: 2, 6: 3, 7: 3, 8: 3... 19: 6 }

Function get AgeGroup(age:Number):int
{
   var intAge:int = Math.floor(age); // if needed
   return int(ageMap[intAge);
}

On 4/9/10 9:03 PM, "Joe Cabezas" <joe.cabe...@gmail.com> wrote:






y did not understand, but i hope i help you

2010/4/9 Wally Kolcz <wko...@isavepets.com>





Thanks for the try, but all ages are > 18 so the first condition would be met 
on all of them. I need the multiple conditions on the case to be preserved. :)



On 4/9/2010 11:45 PM, Rob Romanek wrote:



This is off the top of my head in email so there may be some errors but 
something like this should work




var category:Number;




switch (true){

 case(age > 18):

 category = 0;

 break;


 case(age > 15):

 category = 6;

 break;


 case(age > 12):

 category = 5;

 break;


 case(age > 8):

 category = 4;

 break;


 case(age > 5):

 category = 3;

 break;


 case(age > 2):

 category = 2;

 break;

 default:

 category = 1;

}




return category;




hth,




Rob















On 9-Apr-10, at 10:45 PM, Wally Kolcz wrote:








Can someone rewrite this as a switch/case? I tried, but it didn't seem
to work. Problem I am running into is the multiple conditions in the
case. Thanks!

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;
}

}






















--
Alex Harui
Flex SDK Team
Adobe System, Inc.
http://blogs.adobe.com/aharui

Reply via email to