[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

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;
}
0
BTMExpert
Asked:
BTMExpert
  • 7
  • 3
  • 2
  • +3
1 Solution
 
RoonaanCommented:
if(age > 1 && age < 18) {
} else if(age < 24) {
} else if(age < 30) {
} else {
}

-r-
0
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now