Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shorthand switch statement with conditional OR operator

Is it possible to do that? For exanple for 'a' or 'b' is equal to 'X'. If 'c' or 'd' or 'e' is equal to 'Y'

var qwerty = function() {
    var month = 'a';
    var cases = {
      'a' || 'b' : month = 'X',
      'c' || 'd' || 'e' : month = 'Y'
    };
    if (cases[month]) {
      cases[month]();
    }
    return month;
};

console.log( qwerty() );

Thank you in advance :)

like image 757
Aldren Terante Avatar asked Dec 03 '22 21:12

Aldren Terante


1 Answers

There is no 'or' in a switch statement, as such. But you can stack up your cases like so:

switch(month){
    case 'a':
    case 'b':
        month = 'X';
       break;
    case 'c':
    case 'd':
    case 'e':
        month = 'Y';
        break;
}
like image 140
George Avatar answered Dec 31 '22 05:12

George