Javascript compare 3 values

后端 未结 7 2371
执笔经年
执笔经年 2020-11-29 11:04

I have 3 values that I want to compare f, g and h. I have some code to check that they all equal each other and that none of them are null. I\'ve had a look online but could

相关标签:
7条回答
  • 2020-11-29 11:50

    Write a simple function:

    var checkAllArguments = function() {
        var len = arguments.length;
        var obj;
    
        if(len == 0) {
            return true;
        } else {
            if(arguments[0] == null) {
                return false;
            } else {
                obj = arguments[0];
            }
        }
    
        for(var i=1; i<len; i++) {
            if(arguments[i] == null) {
                return false;
            }
    
            if(obj == arguments[i]) {
                continue;
            } else {
                return false;
            }
        }
    
        return true;
    }
    

    Now, to check multiple arguments, all you have to do is:

    if(checkAllArguments(g, h, f)) {
       // Do something
    }
    
    0 讨论(0)
提交回复
热议问题