Copy link to clipboard
Copied
I have to take a number and figure out which "tier" it falls into.
For example, let's say I have a table that has three tiers "economy" "typical" "deluxe". From 0-12 is economy, 13-20 is typical, 21-30 is deluxe. My actual table has many more levels, but this is the concept.
What's the fastest and easiest way to do that? I was going to use a switch statement starting with the highest, but it doesn't look like that can be done
you can call tierF() passing any number and it will return the tier label:
var labelA:Array=["economy","typical",etc];
var tierA:Array=[[0,12],[13-20],etc];
function tierF(n:Number):String{
for(var i:Number=0;i<tierA.length;i++){
if(n>=tierA[0]&&n<=tierA[1]){
return labelA;
}
}
}
Copy link to clipboard
Copied
if there's no pattern to the tiers, add them to an array and use your array to categorize tiers.
Copy link to clipboard
Copied
I thought of an array, but I'm still not sure how to do the comparison.
Copy link to clipboard
Copied
I suppose I could do it as an ordered array or dictionary
{economy:1},{economy:2}...
Copy link to clipboard
Copied
you can call tierF() passing any number and it will return the tier label:
var labelA:Array=["economy","typical",etc];
var tierA:Array=[[0,12],[13-20],etc];
function tierF(n:Number):String{
for(var i:Number=0;i<tierA.length;i++){
if(n>=tierA[0]&&n<=tierA[1]){
return labelA;
}
}
}
Copy link to clipboard
Copied
Thanks much, I get it now (I think). I had to change your function a bit so it always returns a value:
private var labelA:Array=["economy","typical"]; private var tierA:Array=[[0,12],[13,14,20]]; private function tierF(n:Number):String{ var s:String = ""; for(var i:Number=0;i<tierA.length;i++){ if(n>=tierA[0]&&n<=tierA[1]){ s = labelA; } } return s; }
So trace tierF(14) traces "typical".
I'm assuming your "13-20" was shorthand, since that didn't work as is.
Copy link to clipboard
Copied
you're welcome.
but, each element of tierA needs only 2 elements. and my function is prefered because the loop stops when the correct tier is found. if no tier is found an error message would be even better:
private var labelA:Array=[economy,typical];
private var tierA:Array=[[0,12],[13,20]];
function tierF(n:Number):String {
for (var i:Number=0; i<tierA.length; i++) {
if (n>=tierA[0]&&n<=tierA[1]) {
return labelA;
}
}
return "out of range";
}
Copy link to clipboard
Copied
Oh, I see now. Very clever indeed.