Link to home
Start Free TrialLog in
Avatar of BTMExpert
BTMExpert

asked on

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;
}
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;
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")))
+++++++++++++
Avatar of smaccari
smaccari


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.
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...
>> or better:

I'd argue about "better" ;-)
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-
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
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
@Tim:

I see you already got a better one there.

-regards
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.....
Avatar of BTMExpert

ASKER

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
Switch is simple but needs more code.

condition ? statement1 : statement2 requires less coding

Both give similar results..
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-
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.
@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