Switch Case To/Between

后端 未结 4 872
终归单人心
终归单人心 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条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-03 13:31

    Posting for "cool" syntax :P

    if( integer in range(0, 10 ) ) {
    
    }
    else if ( integer in range( 11, 20 ) ) {
    
    
    
    }
    else if ( integer in range( 21, 30 ) ) {
    
    
    
    }
    
    function range( min, max ){
    var o = {}, i ;
        for( i = min; i <= max; ++i ) {
        o[i] = !0;
        }
    return o;
    }
    

提交回复
热议问题