Multiple options for a single case


#1

Greetings comrades,

The following code runs and executes perfectly. I'm just wondering if adding multiple "options" for a single case is valid js, please see the Toyota case in my code below.

var answer = prompt("What's your fav car brand?","enter car brand here...");

switch(answer) {
  case 'Toyota','toyota':
    console.log("Reliable and RAD!");
    break;
  case 'VW':
    console.log("Can't go wrong with a VDUB!");
    break;
  case 'BMW':
    console.log("BMW doesn't give problems!");
    break;
  default:
    console.log("Looks like you better call Uber..");
    break;
}

#2

Hi @rorygrenade, good question! :smiley:

I have set up a repl for you to see that when you enter "Toyota" you get the output Looks like you better call Uber.. where you would expect - Reliable and RAD!. But, when you enter "toyota" you get the expected output (Reliable...)

So, when the code runs without errors, then why is it that case isn't accepting "Toyota" but only "toyota"?

Turns out that if you swap the order of these two to make it:

switch(answer) {
  case 'toyota', 'Toyota':
    console.log("Reliable and RAD!");

Their corresponding outputs get reversed too. Odd, isn't it?

Well, not really. This is happening because what you are seeing is the effect of the comma operator which evaluates all its operands ("Toyota" and "toyota" in this case) and returns the last one only.

Can you figure out how it is affecting your program's output? :wink:

Hope it helps! :smiley:


#3

Thanks for the SUPER informative answer. Initially I thought it accepted both operands ("Toyota" and "toyota"). but now I see that it only returns the last operand when both are used as a single case.

Now my only question is, what would the most elegant method to create the functionality for the user to be able to enter "Toyota" and "toyota" and get the result "Reliable and RAD!"? Would I have to create two separate cases?


#4

There are two options:

Option 1.

Use fall-through technique - where, in the absence of break, the flow of control falls to the next case following the matched case.

switch(answer) {
  case 'toyota':  // notice no break here
  case 'Toyota':
    console.log("Reliable and RAD!");
    break; // now we have handled toyota, Toyota
  case "some other car"....

But this will fail if the user inputs "ToYoTa" or anything other than 'toyota' and 'Toyota'.

Option 2.

Simply convert the input to lowercase. Like this:

var answer = prompt("What's your fav car brand?","enter car brand here...");
answer = answer.xyz(); // do you know the method to convert string to lowercase? ;)

switch(answer) {
    case 'toyota': // now we don't need Toyota, TOYOTA, and all other relatives
         console.log("Reliable and RAD!");
         break;
    ....

Hope it helps! :smiley:


#5

Wow... this is extremely valuable info, I really appreciate the insight!

So w3schools says that the convert string to lowercase method is as follows:

.toLowerCase();

Looks like a jQuery method :slightly_smiling:


#6

Well, no. It is a native JavaScript method. You can find more info here.

P.S. Reading docs on/Linking to w3schools is discouraged. Read why :slightly_smiling:


#7

Hey gaurangtandon, just to clarify, I didn't mean to say that I think that it is a jQuery method, l meant that it looks like a jQuery method. Anyways thanks for the info, you seem to be a very knowledgeable programmer.