using switch and a number range

how can I use a switch and a number range

something like

    switch(age)
    {
              case(age > 1 and age < 18):
            {
            var imgName = "/images/info.gif";
         break;
      }
                   case(age > 18 and age < 24):
      {
            var imgName = "/images/info2.gif";
         break;
      }
                   case(age > 24 and age < 30):
      {
            var imgName = "/images/info3.gif";
         break;
      }
            default:
      {
            var imgName = "/images/info4.gif";
        break;
      }
   }  // end-switch

document.getElementById("rates").src = imgName;
}
BTMExpertAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RoonaanCommented:
if(age > 1 && age < 18) {
} else if(age < 24) {
} else if(age < 30) {
} else {
}

-r-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
raj3060Commented:
Like this: (not much different than @Roonaan)

   if((age > 1) && (age < 18))
           {
          imgName = "/images/info.gif";        
      }
      else if(age < 24)
      {
         imgName = "/images/info2.gif";        
      }
      else if(age < 30)
      {
         imgName = "/images/info4.gif";        
      }
     else
      {
      imgName = "/images/info4.gif";      
      }  
document.getElementById("rates").value = imgName;
0
raj3060Commented:
or better:

+++++++++++++
(((age > 1) && (age < 18))?imgName = "/images/info.gif":((age < 24)?imgName = "/images/info2.gif":((age < 30)?imgName = "/images/info3.gif":imgName = "/images/info4.gif")))
+++++++++++++
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

smaccariCommented:

You can use switch case only for value exact matching (not for range matching).
So you have to use if else statements in your case, as both solutions above showed you.
0
WelkinMazeCommented:
The short answer is you can't!
The long one is:
You use switch for fixed values not for ranges.
The only chance to use it with ranges is to enumerate all the values in the range like
case 1
case 2
case 3
....

But this is inconvenient as you may see...
0
TimYatesCommented:
>> or better:

I'd argue about "better" ;-)
0
RoonaanCommented:
You can use:

switch(true)
    {
            case(age > 1 and age < 18):
           {
          var imgName = "/images/info.gif";
         break;
      }
                case(age > 18 and age < 24):
      {
          var imgName = "/images/info2.gif";
         break;
      }
                case(age > 24 and age < 30):
      {
          var imgName = "/images/info3.gif";
         break;
      }
           default:
      {
          var imgName = "/images/info4.gif";
        break;
      }
   }  // end-switch


-r-
0
raj3060Commented:
No problem Tim

Here is another:

++++++++++++++++++++++
   var age = 30
   var imgName
   var caseWhat
      
(((age > 1) && (age < 18))?caseWhat = 1:((age < 24)?caseWhat=2:((age < 30)?caseWhat=3:caseWhat=4)))

    switch(caseWhat)
    {
      case(1):
      {
         imgName = "/images/info.gif";
         break;
      }
      case(2):
      {
         imgName = "/images/info2.gif";
         break;
      }
      case(3):
      {
         imgName = "/images/info3.gif";
         break;
      }
      default:
      {
         imgName = "/images/info4.gif";
        break;
      }
   }  // end-switch
document.getElementById("rates").value = imgName;
}
++++++++++++++++++++++

If BTMExpert really want to use SWITCH
0
smaccariCommented:
Nice shot Roonaan!

Just replace the and with && though (of course ;)

switch(true)
    {
            case(age > 1 && age < 18):
           {
          var imgName = "/images/info.gif";
         break;
      }
                case(age > 18 && age < 24):
      {
          var imgName = "/images/info2.gif";
         break;
      }
                case(age > 24 && age < 30):
      {
          var imgName = "/images/info3.gif";
         break;
      }
           default:
      {
          var imgName = "/images/info4.gif";
        break;
      }
   }  // end-switch
0
raj3060Commented:
@Tim:

I see you already got a better one there.

-regards
0
raj3060Commented:
Actually that's @Roonaan................:)


But I would still say

(((age > 1) && (age < 18))?imgName = "/images/info.gif":((age < 24)?imgName = "/images/info2.gif":((age < 30)?imgName = "/images/info3.gif":imgName = "/images/info4.gif")))

is doing what @BTMExpert wants in one line.....
0
BTMExpertAuthor Commented:
well I did it the first way and it looked like it was working

which way is the best way to do it? I thought I should use case because it is like 10 different cases
0
raj3060Commented:
Switch is simple but needs more code.

condition ? statement1 : statement2 requires less coding

Both give similar results..
0
RoonaanCommented:
raj3060

I hope you are not telling me that you find the below line readable, maintainable and the better solution:
((age > 1) && (age < 18))?imgName = "/images/info.gif":((age < 24)?imgName = "/images/info2.gif":((age < 30)?imgName = "/images/info3.gif":imgName = "/images/info4.gif")))

Evenso, when you would want to do it in this syntax, use:
imgName = ((age > 1) && (age < 18))? "/images/info.gif":((age < 24)? "/images/info2.gif":((age < 30)? "/images/info3.gif": "/images/info4.gif")))

And that is even not very sensible.

-r-
0
WelkinMazeCommented:
BTMExpert, you have to compromise between short coding and readability.
It's not good to go too far in either direction. So it's better to think for both.
0
raj3060Commented:
@Roonaan
I am not saying one solution is better than the other. Both work and give similar results.
Which one you want to use depends on how much coding you want to do, and which statement you feel comfirtable with.

I agree with @WelkinMaze.

BTW: Thanks for simplifying the code.

-regards
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.