// checking whether a number is a multiple of 3 or not
for (let number = 1; number <= 100; number++) {
switch (number % 3) {
case !0: // Here I have used !(not) but it's not helping, I only want to know why '!' is not helping
console.log(`${number} is not multiple of 3`);
break;
default:
console.log(`${number} is multiple of 3`)
}
}
Here the 1st case is not working. Code execution has no problems but the 1st case is not helping at all. The complete flow is going to the 'default:' code block only. Whenever the remainder is not equal to 0, the 1st case's code block should be executed, but it's always going to the default code block.
You can't do it in this way because !0 is always true in JavaScript so the loop always enters in the first case
Try it in this way
for (let number = 1; number <= 100 ;number++ ) {
if(number % 3){
console.log(`${number} is not multiple of 3`);
}
else{
console.log(`${number} is multiple of 3`);
}
}
The switch statement receives values on case statements, not expressions like the if. So, when js run your code, it will evaluate !0 to true (inverse of a falsy value is true).
You must replace your switch by if and elses:
for (let number = 1; number <= 100 ;number++ ) {
if ((number % 3) !== 0) {
console.log(`${number} is not multiple of 3`);
} else {
console.log(`${number} is multiple of 3`)
}
}
But you can still use a switch case statement if you invert the logic:
for (let number = 1; number <= 100 ;number++ ) {
switch (number % 3) {
case 0:
console.log(`${number} is multiple of 3`)
break;
default:
console.log(`${number} is not multiple of 3`);
}
}
But really makes no sense using a switch case in this situation since an if-else is much simpler and easier to read.
!0 evals to true, which is not equals to 1 or 2.
Consider writing it in this way:
for (let number = 1; number <= 100; number++) {
switch (number % 3) {
case 0:
console.log(`${number} is multiple of 3`)
break;
default:
console.log(`${number} is not multiple of 3`);
}
}
In JS, 0 is considered falsy that's why !0 will equals true ( not falsy ).
As for your switch, you could reverse your it and use a true as the switch values. The cases would then have the expression to check
for (let number = 1; number <= 100 ;number++ ) {
switch (true) {
case number % 3:
console.log(`${number} is not multiple of 3`);
break;
default:
console.log(`${number} is multiple of 3`)
}
}
Related
How can I use a condition inside a switch statement for JavaScript?
In the example below, a case should match when the variable liCount is <= 5 and > 0; however, my code does not work:
switch (liCount) {
case 0:
setLayoutState("start");
var api = $("#UploadList").data("jsp");
api.reinitialise();
break;
case liCount <= 5 && liCount > 0:
setLayoutState("upload1Row");
var api = $("#UploadList").data("jsp");
api.reinitialise();
break;
case liCount <= 10 && liCount > 5:
setLayoutState("upload2Rows");
var api = $("#UploadList").data("jsp");
api.reinitialise();
break;
case liCount > 10:
var api = $("#UploadList").data("jsp");
api.reinitialise();
break;
default:
break;
}
Appreciate any advice!
This works:
switch (true) {
case liCount == 0:
setLayoutState('start');
var api = $('#UploadList').data('jsp');
api.reinitialise();
break;
case liCount<=5 && liCount>0:
setLayoutState('upload1Row');
var api = $('#UploadList').data('jsp');
api.reinitialise();
break;
case liCount<=10 && liCount>5:
setLayoutState('upload2Rows');
var api = $('#UploadList').data('jsp');
api.reinitialise();
break;
case liCount>10:
var api = $('#UploadList').data('jsp');
api.reinitialise();
break;
}
The only thing necessary is switch(true){...} and for your case expressions to evaluate to booleans.
It works because, the value we give to the switch is used as the basis to compare against. Consequently, the case expressions, also evaluating to booleans will determine which case is run. Could also turn this around, and pass switch(false){..} and have the desired expressions evaluate to false instead of true.. but personally prefer dealing with conditions that evaluate to truthyness. However, it does work too, so worth keeping in mind to understand what it is doing.
Eg: if liCount is 3, the first comparison is true === (liCount == 0), meaning the first case is false. The switch then moves on to the next case true === (liCount<=5 && liCount>0). This expression evaluates to true, meaning this case is run, and terminates at the break. I've added parentheses here to make it clearer, but they are optional, depending on the complexity of your expression.
It's pretty simple, and a neat way (if it fits with what you are trying to do) of handling a long series of conditions, where perhaps a long series of ìf() ... else if() ... else if () ... might introduce a lot of visual noise or fragility.
Use with caution, because it is a non-standard pattern, despite being valid code.
You've way overcomplicated that. Write it with if statements instead like this:
if(liCount == 0)
setLayoutState('start');
else if(liCount<=5)
setLayoutState('upload1Row');
else if(liCount<=10)
setLayoutState('upload2Rows');
$('#UploadList').data('jsp').reinitialise();
Or, if ChaosPandion is trying to optimize as much as possible:
setLayoutState(liCount == 0 ? 'start' :
liCount <= 5 ? 'upload1Row' :
liCount <= 10 ? 'upload2Rows' :
null);
$('#UploadList').data('jsp').reinitialise();
You want to use if statements:
if (liCount === 0) {
setLayoutState('start');
} else if (liCount <= 5) {
setLayoutState('upload1Row');
} else if (liCount <= 10) {
setLayoutState('upload2Rows');
}
$('#UploadList').data('jsp').reinitialise();
See dmp's answer below. I'd delete this answer if I could, but it was accepted so this is the next best thing :)
You can't. JS Interpreters require you to compare against the switch statement (e.g. there is no "case when" statement). If you really want to do this, you can just make if(){ .. } else if(){ .. } blocks.
You can use fall-through method in switch case.
const x = 'Welcome';
switch (x) {
case 'Come':
console.log(1)
break;
case 'Welcome':
case 'Wel':
case 'come':
console.log(2)
break;
case 'Wel':
console.log(3)
break;
default:
break;
}
> Result => 2
switch (true) {
case condition0:
...
break;
case condition1:
...
break;
}
will work in JavaScript as long as your conditions return proper boolean values, but it doesn't have many advantages over else if statements.
if the possible values are integers you can bunch up cases.
Otherwise, use ifs.
var api, tem;
switch(liCount){
case 0:
tem= 'start';
break;
case 1: case 2: case 3: case 4: case 5:
tem= 'upload1Row';
break;
case 6: case 7: case 8: case 9: case 10:
tem= 'upload2Rows';
break;
default:
break;
}
if(tem) setLayoutState((tem);
api= $('#UploadList').data('jsp');
api.reinitialise();
That's a case where you should use if clauses.
If that's what you want to do, it would be better to use if statements. For example:
if(liCount == 0){
setLayoutState('start');
}
if(liCount<=5 && liCount>0){
setLayoutState('upload1Row');
}
if(liCount<=10 && liCount>5){
setLayoutState('upload2Rows');
}
var api = $('#UploadList').data('jsp');
api.reinitialise();
Your code does not work because it is not doing what you are expecting it to do. Switch blocks take in a value, and compare each case to the given value, looking for equality. Your comparison value is an integer, but most of your case expressions resolve to a boolean value.
So, for example, say liCount = 2. Your first case will not match, because 2 != 0. Your second case, (liCount<=5 && liCount>0) evaluates to true, but 2 != true, so this case will not match either.
For this reason, as many others have said, you should use a series of if...then...else if blocks to do this.
Notice that we don't pass score to the switch but true. The value we give to the switch is used as the basis to compare against.
The below example shows how we can add conditions in the case: without any if statements.
function getGrade(score) {
let grade;
// Write your code here
switch(true) {
case score >= 0 && score <= 5:
grade = 'F';
break;
case score > 5 && score <= 10:
grade = 'E';
break;
case score > 10 && score <= 15:
grade = 'D';
break;
case score > 15 && score <= 20:
grade = 'C';
break;
case score > 20 && score <= 25:
grade = 'B';
break;
case score > 25 && score <= 30:
grade = 'A';
break;
}
return grade;
}
If you want pass any value in switch statement
and then apply condition on that passing value and
evaluate statement then you have to write switch
statement under an function and pass parameter in that
function and then pass true in switch expression like the
below example.
function numberChecker(num){
let age;
switch(true){
case num >= 0 && num <= 10:
age = "Child";
break;
case num >= 10 && num <= 20:
age = "Teenager";
break;
case num >= 20 && num <= 30:
age = "Young";
break;
default:
age = "Undefined!! Enter Age Between 0 - 30";
break;
}
console.log("WOW You Are " + age);
}
numberChecker(15);
Although in the particular example of the OP's question, switch is not appropriate, there is an example where switch is still appropriate/beneficial, but other evaluation expressions are also required. This can be achieved by using the default clause for the expressions:
switch (foo) {
case 'bar':
// do something
break;
case 'foo':
// do something
break;
... // other plain comparison cases
default:
if (foo.length > 16) {
// something specific
} else if (foo.length < 2) {
// maybe error
} else {
// default action for everything else
}
}
The below function is working as expected except case noticed in a comment, namely, for each number:
if the number is divisible by three I want to log out the word fizz instead of that number
if the number is divisible by five I want to log out the word buzz instead of that number.
And if a number is divisible by both 3 and 5 I want to log out the word fizzbuzz instead of that number.
Example - 1
const FizzBuzz = (num) => {
for (let i = 1; i <= num; i++) {
const foo = 0;
switch (foo) {
case (i % 3 || i % 5): console.log('fizzbuzz');
// expect in this case should be work with logical operator '&&': (i % 3 && i % 5)
break;
case i % 3: console.log('fizz');
break;
case i % 5: console.log('buzz');
break;
default: console.log(i);
}
}
}
FizzBuzz(20)
This is how I know - With switch-case statement preferable use:
Example 2
switch(true) {
case (i % 3 === 0 && i % 5 === 0): console.log('fizzbuzz');
break;
// etc...
}
But with the instance of code above (Example 1: where switch(foo) doesn't have the constant value like true), it looks more flexible and readable.
And in "Example 1", as I understood, the operator || works like operator && and vice versa.
The "Example 1" works perfectly as result. But I can't understand Why. Primarily my question is - Why the logical operator OR behave as an operator AND in this example??
Thnx in advance.
The issue is that you're switching on foo, and foo is 0, so case <expression> will run when <expression> evaluates to 0.
So, case i % 3 runs when expected, because you want the i % 3 case to run when that evaluates to 0. Same for i % 5. But
case (i % 3 || i % 5)
, when i is a multiple of 15, resolves to
case (15 % 3 || 15 % 5)
->
case (0 || 0)
Because 0 is falsey, the || will mean that both 15 % 3 and 15 % 5 must be 0 for the case there to resolve to 0 (and thus match foo's 0).
If you used &&, then the && expression will evaluate to the first falsey value, rather than requiring both %s to resolve to 0, eg when i is 3:
case (3 % 3 && 3 % 5)
->
case (0 && 2)
->
case 0
which then matches foo's 0, despite the fact that only one of the conditions in the && resolved to 0.
It's extremely unintuitive. Don't use switch here. If you had to use switch, I'd highly recommend setting foo to true instead, and using === 0 tests:
const FizzBuzz = (num) => {
for (let i = 1; i <= num; i++) {
const foo = true;
switch (foo) {
case i % 3 === 0 && i % 5 === 0:
console.log('fizzbuzz');
break;
case i % 3 === 0:
console.log('fizz');
break;
case i % 5 === 0:
console.log('buzz');
break;
default:
console.log(i);
}
}
}
FizzBuzz(20)
The code in Example 1 works because you switch on the variable foo which is set to 0.
The code in the first case is evaluated to 0:
15 % 3 || 15 % 5 = 0
This value gets compared to foo which is 0 and so the JS code enters the first case.
In JavaScript logical OR operators always return the first "truthy" value.
'foo' || 'bar' = 'foo'
false || 'bar' = 'bar'
I have the following code displaying an image based on gamma values of the gyroscope. My first shot at it was to write a switch statement but having used object literals before I thought this could be a cleaner alternative. Is there any way to do this with the following code? Or any other cleaner solution?
switch (true) {
case (gamma <= -28):
view360.goToItem(0);
break;
case (gamma <= -24):
view360.goToItem(1);
break;
case (gamma <= -20):
view360.goToItem(2);
break;
case (gamma <= -16):
view360.goToItem(3);
break;
case (gamma <= -12):
view360.goToItem(4);
break;
case (gamma <= -8):
view360.goToItem(5);
break;
case (gamma <= -4):
view360.goToItem(6);
break;
case (gamma <= 0):
view360.goToItem(7);
break;
case (gamma <= 4):
view360.goToItem(8);
break;
case (gamma <= 8):
view360.goToItem(9);
break;
case (gamma <= 12):
view360.goToItem(10);
break;
case (gamma <= 16):
view360.goToItem(11);
break;
case (gamma <= 20):
view360.goToItem(12);
break;
case (gamma <= 24):
view360.goToItem(13);
break;
default:
view360.goToItem(13);
}
Your indexes are a function of the gamma, so you should write it as a function that captures that relationship. It looks like the relationship is simply (28 + gamma) / 4 with an additional check gamma is greater than 60. Since you are using inequalities to capture the in-between values, you need to divide by 31 and take the floor. This will allow both 3 and 4 to return 8 for example. So this should match your switches:
function getIndex(g) {
return g > 60 ? 13 : Math.floor((31 + g) / 4)
}
view360.goToItem(getIndex(gamma))
Not in this case, because you're using <= rather than =. Your whole method here would be better expressed with if and else - switch(true) is not really a switch.
Here's a switch you could convert to an object literal:
switch ( val ) {
case 'a': return 'hello';
case 'b': return 'goodbye';
}
Could be:
return { a: 'hello', b: 'goodbye' }[ val ];
Because the result of your switch (the argument to goToItem) is sequential (0, 1, 2...) you could use an array for this.
var gammaValues = [ -28, -24, -20, -16 /* etc */ ];
var idx = gammaValues.findIndex( value => gamma <= value );
if ( index !== -1 ) view360.goToItem( idx );
May be using map can help
const mapBreakpointToItem = {
-28: 0,
-24: 1,
...
};
Object.keys(mapBreakpointToItem).some((breakpoint) => {
if (gamma <= breakpoint) {
const item = mapBreakpointToItem[breakpoint];
view360.goToItem(item);
return true;
}
return false;
});
Or you can use math Math.floor((gamma + 31) / 4 for mapping breakpoints and items, but if something is changed its easier to change map object.
I'm currently taking the code academy course on Javascript and I'm stuck on the FizzBuzz task. I need to count from 1-20 and if the number is divisible by 3 print fizz, by 5 print buzz, by both print fizzbuzz, else just print the number. I was able to do it with if/ else if statements, but I wanted to try it with switch statements, and cannot get it. My console just logs the default and prints 1-20. Any suggestions?
for (var x = 0; x<=20; x++){
switch(x){
case x%3==0:
console.log("Fizz");
break;
case x%5===0:
console.log("Buzz");
break;
case x%5===0 && x%3==0:
console.log("FizzBuzz");
break;
default:
console.log(x);
break;
};
};
Switch matches the x in switch(x){ to the result of evaluating the case expressions. since all your cases will result in true /false there is no match and hence default is executed always.
now using switch for your problem is not recommended because in case of too many expressions there may be multiple true outputs thus giving us unexpected results. But if you are hell bent on it :
for (var x = 0; x <= 20; x++) {
switch (true) {
case (x % 5 === 0 && x % 3 === 0):
console.log("FizzBuzz");
break;
case x % 3 === 0:
console.log("Fizz");
break;
case x % 5 === 0:
console.log("Buzz");
break;
default:
console.log(x);
break;
}
}
I thought switch too,but no need.
for (var n = 1; n <= 100; n++) {
var output = "";
if (n % 3 == 0)
output = "Fizz";
if (n % 5 == 0)
output += "Buzz";
console.log(output || n);
}
Switch statement checks if the situation given in the cases matches the switch expression. What your code does is to compare whether x divided by 3 or 5 is equal to x which is always false and therefore the default is always executed. If you really want to use a switch statement here is one way you can do.
for (var i=1; i<=30; i++){
switch(0){
case (i % 15):
console.log("fizzbuzz");
break;
case (i % 3):
console.log("fizz");
break;
case (i % 5):
console.log("buzz");
break;
default:
console.log(i);
}
}
Not to too my own horn but this is much cleaner:
var numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
for (var i = 1; i <= numbers.length; i++) {
if (i % 15 === 0) {
console.log("FizzBuzz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else {
console.log(i);
}
};
The switch(true) part of this statement helped me. I was trying to do a switch statement for fizzbuzz. My solution incorporates the coding style of Rosettacodes - general solution. Most significantly the use of force typing to shorten the primary conditionals. I thought, it was valuable enough to post:
var fizzBuzzSwitch = function() {
for (var i =0; i < 101; i++){
switch(true) {
case ( !(i % 3) && !(i % 5) ):
console.log('FizzBuzz');
break;
case ( !(i % 3) ):
console.log('Fizz');
break;
case ( !(i % 5) ):
console.log('Buzz');
break;
default:
console.log(i);
}
}
}
Here's what made it clear for me, might help :
It's a misinterpretation of what switch (x){} means.
It doesn't mean : "whenever whatever I put inbetween those brackets is true, when the value of x changes."
It means : "whenever x EQUALS what I put between those brackets"
So, in our case, x NEVER equals x%3===0 or any of the other cases, that doesn't even mean anything. x just equals x all the time. That's why the machine just ignores the instruction. You are not redefining x with the switch function. And what you put inbetween the brackets describes x and x only, not anything related to x.
In short :
With if/else you can describe any condition.
With switch you can only describe the different values taken by the variable x.
Here's a solution incorporating #CarLuvr88's answer and a switch on 0:
let fizzBuzz = function(min, max){
for(let i = min; i <= max; i++){
switch(0){
case i % 15 : console.log('FizzBuzz'); break;
case i % 3 : console.log('Fizz'); break;
case i % 5 : console.log('Buzz'); break;
default : console.log(i); break;
}
}
}
fizzBuzz(1,20)
We can use a function to find a multiple of any number and declare two variables to identify these multiples so that if you want to change the multiples you only need to change at max 2 lines of code
function isMultiple(num, mod) {
return num % mod === 0;
}
let a = 3;
let b = 5;
for(i=0;i<=100;i++){
switch(true){
case isMultiple(i,a) && isMultiple(i,b):
console.log("FizzBuzz")
case isMultiple(i,a):
console.log("Fizz");
case isMultiple(i,b):
console.log("Buzz");
default:
console.log(i);
}
}
How can I use a condition inside a switch statement for JavaScript?
In the example below, a case should match when the variable liCount is <= 5 and > 0; however, my code does not work:
switch (liCount) {
case 0:
setLayoutState("start");
var api = $("#UploadList").data("jsp");
api.reinitialise();
break;
case liCount <= 5 && liCount > 0:
setLayoutState("upload1Row");
var api = $("#UploadList").data("jsp");
api.reinitialise();
break;
case liCount <= 10 && liCount > 5:
setLayoutState("upload2Rows");
var api = $("#UploadList").data("jsp");
api.reinitialise();
break;
case liCount > 10:
var api = $("#UploadList").data("jsp");
api.reinitialise();
break;
default:
break;
}
Appreciate any advice!
This works:
switch (true) {
case liCount == 0:
setLayoutState('start');
var api = $('#UploadList').data('jsp');
api.reinitialise();
break;
case liCount<=5 && liCount>0:
setLayoutState('upload1Row');
var api = $('#UploadList').data('jsp');
api.reinitialise();
break;
case liCount<=10 && liCount>5:
setLayoutState('upload2Rows');
var api = $('#UploadList').data('jsp');
api.reinitialise();
break;
case liCount>10:
var api = $('#UploadList').data('jsp');
api.reinitialise();
break;
}
The only thing necessary is switch(true){...} and for your case expressions to evaluate to booleans.
It works because, the value we give to the switch is used as the basis to compare against. Consequently, the case expressions, also evaluating to booleans will determine which case is run. Could also turn this around, and pass switch(false){..} and have the desired expressions evaluate to false instead of true.. but personally prefer dealing with conditions that evaluate to truthyness. However, it does work too, so worth keeping in mind to understand what it is doing.
Eg: if liCount is 3, the first comparison is true === (liCount == 0), meaning the first case is false. The switch then moves on to the next case true === (liCount<=5 && liCount>0). This expression evaluates to true, meaning this case is run, and terminates at the break. I've added parentheses here to make it clearer, but they are optional, depending on the complexity of your expression.
It's pretty simple, and a neat way (if it fits with what you are trying to do) of handling a long series of conditions, where perhaps a long series of ìf() ... else if() ... else if () ... might introduce a lot of visual noise or fragility.
Use with caution, because it is a non-standard pattern, despite being valid code.
You've way overcomplicated that. Write it with if statements instead like this:
if(liCount == 0)
setLayoutState('start');
else if(liCount<=5)
setLayoutState('upload1Row');
else if(liCount<=10)
setLayoutState('upload2Rows');
$('#UploadList').data('jsp').reinitialise();
Or, if ChaosPandion is trying to optimize as much as possible:
setLayoutState(liCount == 0 ? 'start' :
liCount <= 5 ? 'upload1Row' :
liCount <= 10 ? 'upload2Rows' :
null);
$('#UploadList').data('jsp').reinitialise();
You want to use if statements:
if (liCount === 0) {
setLayoutState('start');
} else if (liCount <= 5) {
setLayoutState('upload1Row');
} else if (liCount <= 10) {
setLayoutState('upload2Rows');
}
$('#UploadList').data('jsp').reinitialise();
See dmp's answer below. I'd delete this answer if I could, but it was accepted so this is the next best thing :)
You can't. JS Interpreters require you to compare against the switch statement (e.g. there is no "case when" statement). If you really want to do this, you can just make if(){ .. } else if(){ .. } blocks.
You can use fall-through method in switch case.
const x = 'Welcome';
switch (x) {
case 'Come':
console.log(1)
break;
case 'Welcome':
case 'Wel':
case 'come':
console.log(2)
break;
case 'Wel':
console.log(3)
break;
default:
break;
}
> Result => 2
switch (true) {
case condition0:
...
break;
case condition1:
...
break;
}
will work in JavaScript as long as your conditions return proper boolean values, but it doesn't have many advantages over else if statements.
if the possible values are integers you can bunch up cases.
Otherwise, use ifs.
var api, tem;
switch(liCount){
case 0:
tem= 'start';
break;
case 1: case 2: case 3: case 4: case 5:
tem= 'upload1Row';
break;
case 6: case 7: case 8: case 9: case 10:
tem= 'upload2Rows';
break;
default:
break;
}
if(tem) setLayoutState((tem);
api= $('#UploadList').data('jsp');
api.reinitialise();
That's a case where you should use if clauses.
If that's what you want to do, it would be better to use if statements. For example:
if(liCount == 0){
setLayoutState('start');
}
if(liCount<=5 && liCount>0){
setLayoutState('upload1Row');
}
if(liCount<=10 && liCount>5){
setLayoutState('upload2Rows');
}
var api = $('#UploadList').data('jsp');
api.reinitialise();
Your code does not work because it is not doing what you are expecting it to do. Switch blocks take in a value, and compare each case to the given value, looking for equality. Your comparison value is an integer, but most of your case expressions resolve to a boolean value.
So, for example, say liCount = 2. Your first case will not match, because 2 != 0. Your second case, (liCount<=5 && liCount>0) evaluates to true, but 2 != true, so this case will not match either.
For this reason, as many others have said, you should use a series of if...then...else if blocks to do this.
Notice that we don't pass score to the switch but true. The value we give to the switch is used as the basis to compare against.
The below example shows how we can add conditions in the case: without any if statements.
function getGrade(score) {
let grade;
// Write your code here
switch(true) {
case score >= 0 && score <= 5:
grade = 'F';
break;
case score > 5 && score <= 10:
grade = 'E';
break;
case score > 10 && score <= 15:
grade = 'D';
break;
case score > 15 && score <= 20:
grade = 'C';
break;
case score > 20 && score <= 25:
grade = 'B';
break;
case score > 25 && score <= 30:
grade = 'A';
break;
}
return grade;
}
If you want pass any value in switch statement
and then apply condition on that passing value and
evaluate statement then you have to write switch
statement under an function and pass parameter in that
function and then pass true in switch expression like the
below example.
function numberChecker(num){
let age;
switch(true){
case num >= 0 && num <= 10:
age = "Child";
break;
case num >= 10 && num <= 20:
age = "Teenager";
break;
case num >= 20 && num <= 30:
age = "Young";
break;
default:
age = "Undefined!! Enter Age Between 0 - 30";
break;
}
console.log("WOW You Are " + age);
}
numberChecker(15);
Although in the particular example of the OP's question, switch is not appropriate, there is an example where switch is still appropriate/beneficial, but other evaluation expressions are also required. This can be achieved by using the default clause for the expressions:
switch (foo) {
case 'bar':
// do something
break;
case 'foo':
// do something
break;
... // other plain comparison cases
default:
if (foo.length > 16) {
// something specific
} else if (foo.length < 2) {
// maybe error
} else {
// default action for everything else
}
}