8: Switch and logical comparison


#1

8: Switches all on your own

On the last exercise for switches I tried to be a littel more elaborate and used cases with logical comparisons, like I would to in "if" statements.

However for some reason the code always returns the default message.

According to stack overflow swichtes and cases are very well able to handel comparisons like x>2 etc.

So I wonder, why my code is just not working and always showing the default message.

var number = 1;


switch (number) {
    
    case (number<=2 && number>0):
        console.log("<2");
        break;
        
    case (number<=3 && number>2):
        console.log("<3");
        break;
        
    case (number<=4 && number>3):
        console.log("<4");
        break;
        
        default:
        console.log("default");
}

#2

this doesn't work, because of how case works, as explained here, and it also explains how to use fall-through to do this


#3

he link your provided was helpfull in general, but had nothing to do with my code.

Fallthrough has no use in my code since I did not want to check for locial "OR" connections e.g. using the same console.log statement for more than one case. I was checking for a special value range e.g. between 2 and 4.

Also even if the switch command runs from top to bottom and just executing the first "case" it finds to be true, (which is good to know so still thanks for the link provided) it should still work in my code due to the way I structured my cases and the values to check for from low to high. And even if I would not have structured it that way - als long as I provide a number which is smaller than 4, it sould still find one of the cases to be true. It might not be the correct case depending on the structure of my code, but it should NOT just log the default message in any case.


#4

nope, but it shows you can only use or and not and. Which means if you want to do this, you need to use if, else if and else.

i think you misunderstood this one:

if this is your case:

case (number<=2 && number>0):

this is the first case:

case (number<=2):

it wasn't referring to the order from top to bottom but rather the left hand and right hand side of your and operator.


#5

Thank your for clearing that up about the order in which the switch is working off the arguments.

However I just found this topic on stack exchange where they are using AND in switch cases:

stackoverflow.com/questions/5464362/javascript-using-a-condition-in-switch-case

(had to remove the http since I am a new user)

However stack exchange seems to constantly contradict itself on this issue sine there are a number of other topics on stack exchange, which will tell you, that neither of the logical operators will work and others even tell you, that not even comparison operators work...


#6

okay, i find switch strange now, see what they did here:

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;                  
}

no? Look at switch(true), they just pass a boolean value into the switch, and then it seems you can use && for cases:

var number = 1;


switch (true) {
    case number<=2 && number>0:
        console.log("<2");
        break;
        
    case (number<=3 && number>2):
        console.log("<3");
        break;
        
    case (number<=4 && number>3):
        console.log("<4");
        break;
        
        default:
        console.log("default");
}

interestingly, now your code works as well. I think many people don't know this is possible, and still answer the question (they explain in the stack question why this works)


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.