Is there anyway to implement XOR in javascript

前端 未结 18 688
孤城傲影
孤城傲影 2020-12-24 10:53

I\'m trying to implement XOR in javascript in the following way:

   // XOR validation
   if ((isEmptyString(firstStr) && !isEmptyString(secondStr)) |         


        
18条回答
  •  天命终不由人
    2020-12-24 11:58

    here's an XOR that can accommodate from two to many arguments

    function XOR() {
        for (var i = 1; i < arguments.length; i++) 
            if ( arguments[0] != arguments[i] ) 
                return false; 
        return true; 
    }
    

    Example of use:

    if ( XOR( isEmptyString(firstStr), isEmptyString(secondStr) ) ) {
        alert(SOME_VALIDATION_MSG);
        return;
    }
    

提交回复
热议问题