简体   繁体   中英

(How) Can i use 'or' in a switch statement?

Is the following code right:

var user = prompt("Is programming awesome?").toUpperCase();
switch (user) {
    case 'HELL YEAH' || 'YES' || 'YEAH' || 'YEP' || 'YEA':
        console.log("That's what I'd expected!");
    break;
    case 'KINDA'||'CANT SAY':
        console.log("Oh, you'd like it when you'll be a pro!");
    break;
    case 'HELL NO'||'NO'||'NAH'||'NOPE'||'NA':
        console.log("You can't say that!");
    break;
    default:
        console.log("Wacha tryna say mate?");
    break;
}

I tried it. But it didn't work as I wanted it to. It worked well if the input was 'hell no' or 'hell yeah', but all the later strings were just ignored!

I would approach this using a dictionary (object) containing your answers which you can then check with a function in the switch statement. It's a lot neater than having multiple case checks:

var user = prompt("Is programming awesome?").toUpperCase(); // 'HELL YEAH';

// 'dictionary' is just a JS object that has key/value pairs
// In this instance we have keys that map the case checks you were
// making and each has a corresponding array for its value, filled with
// the OR checks you wanted to make.
// We pass 'dict' in as the second parameter when we call checkAnswer
var dict = {
    yes: ['HELL YEAH', 'YES', 'YEAH', 'YEP', 'YEA'],
    maybe: ['KINDA', 'CANT SAY'],
    no: ['HELL NO', 'NO', 'NAH', 'NOPE', 'NA']
};

// checkAnswer returns 'yes' for the user prompt 'HELL YEAH'
// It loops over the 'dict' object and if it finds an instance of
// your user input in one of the arrays it returns the key
// in this instance 'yes' for 'HELL YEAH' (indexOf returns 0)
// otherwise it returns false (for your 'default' case)
function checkAnswer(user, dict) {
    for (var p in dict) {
        if (dict[p].indexOf(user) > -1) return p;
    }
    return false;
}

// All we do is use checkAnswer to return the key where
// the user input is found in one of the dictionary arrays.
// That will always be a single string which is what switch expects
switch (checkAnswer(user, dict)) {
    case 'yes':
        console.log("That's what I'd expected!");
        break;
    case 'maybe':
        console.log("Oh, you'd like it when you'll be a pro!");
        break;
    case 'no':
        console.log("You can't say that!");
        break;
    default:
        console.log("Wacha tryna say mate?");
        break;
}

DEMO

I guess you have to do :

var user = prompt("Is programming awesome?").toUpperCase();
  switch (user) {
    case 'HELL YEAH':
    case 'YES':
    case 'YEAH':
    case 'YEP':
    case 'YEA':
      console.log("That's what I'd expected!");
      break;
   case 'KINDA':
      ...
}

And so on...

The Javascript switch has a fall-through behaviour, you can leverage this to obtain what you described:

var user = prompt("Is programming awesome?").toUpperCase();
switch (user) {
    case 'HELL YEAH':
    case 'YES':
    case 'YEAH':
    case 'YEP'
    case 'YEA':
        console.log("That's what I'd expected!");
        break;
    case 'KINDA':
    case 'CANT SAY':
        console.log("Oh, you'd like it when you'll be a pro!");
        break;
    case 'HELL NO':
    case 'NO':
    case 'NAH':
    case 'NOPE':
    case 'NA':
        console.log("You can't say that!");
        break;
    default:
        console.log("Wacha tryna say mate?");
        break;
}

Not fancy as using an or operator but it works. With fall-through, execution of the specific case will go down skipping the not matching ones in the same group until some real code is reached.

No, but in C++, Java etc you can do the following:

switch (a_variable) {
   case CONST_1:
   case CONST_2:
   case CONST_3:
       do_something();
       break;
   case CONST_4:
       do_some_stuff();
       break;
   dafeult:
       do_default_stuff();
       break;
}

This is like an or for switch-case . This should work in JS too ^^

Group cases without break.

var user = prompt("Is programming awesome?").toUpperCase();
    switch (user) {
        case 'HELL YEAH':
        case 'YES':
        case 'YEAH':
        case 'YEP':
        case 'YEA':
            console.log("That's what I'd expected!");
        break;
        case 'KINDA':
        case 'CANT SAY':
            console.log("Oh, you'd like it when you'll be a pro!");
        break;
        case 'HELL NO':
        case 'NO':
        case 'NAH':
        case 'NOPE':
        case 'NA':
            console.log("You can't say that!");
        break;
        default:
            console.log("Wacha tryna say mate?");
        break;
    }

I believe the switch has to match its cases to a String, so the || operator wouldn't work.

You can however have multiple cases define the same outcome, which you can do by ommitting the break; .

switch(word) {
  case "yes":
  case "yeah":
    goThroughWithIt();
    break;

  case "no":
  case "nuh uh":
    abort();
    break;
}

I think you should modify as follows:

var user = prompt("Is programming awesome?").toUpperCase();
switch (user) {
    case 'HELL YEAH':
    case 'YES': 
    case 'YEAH':
    case 'YEP': 
    case 'YEA':
      console.log("That's what I'd expected!");
    break;

    case 'KINDA':
    case 'CANT SAY':
        console.log("Oh, you'd like it when you'll be a pro!");
    break;

    case 'HELL NO':
    case'NO':
    case'NAH':
    case'NOPE':
    case'NA':
        console.log("You can't say that!");
    break;

    default:
        console.log("Wacha tryna say mate?");
    break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM