Multiple conditions in an if clause

后端 未结 9 1038
醉梦人生
醉梦人生 2021-02-02 06:44

If I have an if statement that needs to meet these requirements:

if(cave > 0 && training > 0 && mobility > 0 && sleep > 0)
         


        
9条回答
  •  伪装坚强ぢ
    2021-02-02 07:16

    Sounds like a job for a "validator" function like this:

    function areAllGreaterThanZero(){
        //TODO: check inputs
        var result = true;
        [].splice.apply(arguments).forEach(function(x){ 
            result = result && (x > 0); 
        });
        return result;
    }
    
    if(areAllGreaterThanZero(cave, training, mobility, sleep)) {
        // ...
    }
    

提交回复
热议问题