shorthand switch statement with conditional OR operator
javascript, jquery
Solution
[2022 updated the answer]
Not sure what your method should return (now it simply returns 'a'). This is a possible rewrite, to demonstrate 'switching' using shortcut boolean evaluation.
With regard to @John Pace's comment/answer, I deviced a little test @Stackblitz
// original answer, simplified
const qwerty = m =>
/[ab]/.test(m) && 'X' ||
/[cde]/i.test(m) && 'Y' || 'NOPES';
// stacked switch
const qwerty1 = m => {
switch (m) {
case `a`:
case 'b':
return `X`;
case 'c':
case 'd':
case 'e':
return `Y`;
default:
return `NOPES`;
}
};
console.log(qwerty('b'));
console.log(qwerty('e'));
console.log(qwerty('x'));
console.log(qwerty1('b'));
console.log(qwerty1('e'));
console.log(qwerty1('x'));
.as-console-wrapper {
max-height: 100% !important;
}
Problem
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 :)