Switch Case To/Between

后端 未结 4 898
终归单人心
终归单人心 2021-01-03 12:56

Is there a way in Javascript to compare one integer with another through switch case structures without using if statements?

E.g.

switch(integer) {
         


        
4条回答
  •  旧时难觅i
    2021-01-03 13:27

    As stated in my comment, you can't do that. However, you could define an inRange function:

    function inRange(x, min, max) {
        return min <= x && x <= max;
    }
    

    and use it together with if - else if. That should make it quite easy to read:

    if(inRange(integer, 1, 10)) {
    
    }
    else if(inRange(integer, 11, 20)) {
    
    }
    
    //...
    

提交回复
热议问题