Multiple conditions in an if clause

后端 未结 9 1039
醉梦人生
醉梦人生 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:10

    Filter it with lodash:

    var data = [cave, training, mobility, sleep];
    var result = _.filter(data, function (datum) { return datum > 0; }).length === data.length;
    
    console.log(result);
    

    It iterates over array elements and returns new array composed of those elements that meet given requirement of being > 0 - if result array is different size than given one it means one or more of it's elements were not > 0.

    I wrote it specifically to check every array value (even if not necessary as first > 0 could give same result) to not stop on first positive as you stated you want to check all of them.

    PS You can reverse it to check for <= 0 and check for .length === 0 instaed to be faster.

    0 讨论(0)
  • 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)) {
        // ...
    }
    
    0 讨论(0)
  • 2021-02-02 07:17

    You could get the lowest value with Math.min, and then you only need one check against the lower bound.

    if(Math.min(cave, training, mobility, sleep) > 0) {
        //do something
    }
    
    0 讨论(0)
  • 2021-02-02 07:19

    Why you are looking for solution?

    Your question looks like best and simple answer, i recommend it. We have multiple solutions for this. Below one is that.

    JSBin for .every()

    Achieve it by using .every function

    var flag = [cave, training, mobility, sleep].every(function(val) {
         return val > 0;
      });
    
    if(flag) {
      alert('All the elements are greater than Zero');
    }
    
    0 讨论(0)
  • 2021-02-02 07:26

    There is nothing wrong with having multiple, simple conditions in an if statement. However, if it cannot fit into a single line (about 80 characters), you have a few solutions.

    Ultimately, you are not checking whether four variables are greater than zero. You are checking for a set of conditions. The fact that these conditions are (currently) represented by signed integers is not only irrelevant, but is an implementation details that should be hidden away in functions.

    1. Use intermediary flags:

      var valid_location = false;
      if (cave > 0 && training > 0)
          valid_location = true;
      
      var valid_status = false;
      if (mobility > 0 && sleep > 0)
          valid_status = true;
      
      if (valid_location && valid_status)
          // ...
      
    2. Use a function:

      function can_do_this()
      {
          // split conditions into logical groups
      
          // checking location, because you need training if you're
          // in a cave
          if (cave <= 0 || training <= 0)
              return false;
      
          // checking status, because you have to be mobile and
          // sleepy
          if (mobility <= 0 || sleep <= 0)
              return false;
      
          return true;
      }
      
      if (can_do_this())
          // ...
      
    3. Use functions for the individual conditions you need to check:

      function valid_location()
      {
          return (cave > 0 && training > 0);
      }
      
      function valid_status()
      {
          return (mobility > 0 && sleep > 0);
      }
      
      if (valid_location() && valid_status())
          // ...
      
    0 讨论(0)
  • 2021-02-02 07:26

    As others have suggested, you can use .every if you don't mind using ES6 or polyfills:

    var hasAllStats = [cave, training, mobility, sleep]
      .every(function(stat) { return stat > 0; });
    
    if (hasAllStats) { }
    

    Alternatively, you can use .some to get the inverse (Also requires ES6 or polyfill):

    var isMissingStats = [cave, training, mobility, sleep]
      .some(function(stat) { return stat <= 0; });
    
    if (!isMissingStats) { }
    

    If you don't want to use ES6, you can use reduce:

    var hasAllStats = [cave, training, mobility, sleep]
      .reduce(function(hasAllStats, stat) {
        return hasAllStats && stat > 0;
      }, true);
    
    if (hasAllStats) { }
    
    0 讨论(0)
提交回复
热议问题