Is there anyway to implement XOR in javascript

前端 未结 18 764
孤城傲影
孤城傲影 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

    @george, I like your function for its capability to take in more than 2 operands. I have a slight improvement to make it return faster:

    function xor() {
        for (var i=arguments.length-1, trueCount=0; i>=0; --i)
            if (arguments[i]) {
                if (trueCount)
                    return false
                ++trueCount;
            }
        return trueCount & 1;
    }
    

提交回复
热议问题