• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Find which range bracket/tier a number falls into

Engaged ,
Mar 29, 2010 Mar 29, 2010

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

TOPICS
ActionScript

Views

1.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 29, 2010 Mar 29, 2010

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;

}

}

}

Votes

Translate

Translate
Community Expert ,
Mar 29, 2010 Mar 29, 2010

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 29, 2010 Mar 29, 2010

Copy link to clipboard

Copied

I thought of an array, but I'm still not sure how to do the comparison.

Not sure if I'm explaining it correctly, but I would have a number and then need to know which tier that number falls into (out of 3 in my example).
If I make an array out of the tiers [economy,typical,deluxe] how would I know if the user types "12" which member of the array to return?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 29, 2010 Mar 29, 2010

Copy link to clipboard

Copied

I suppose I could do it as an ordered array or dictionary

{economy:1},{economy:2}...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 29, 2010 Mar 29, 2010

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;

}

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 30, 2010 Mar 30, 2010

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 30, 2010 Mar 30, 2010

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 30, 2010 Mar 30, 2010

Copy link to clipboard

Copied

LATEST

Oh, I see now. Very clever indeed.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines