Is there anyway to implement XOR in javascript

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

    Assuming you are looking for the BOOLEAN XOR, here is a simple implementation.

    function xor(expr1, expr2){
        return ((expr1 || expr2) && !(expr1 && expr2));
    }
    

    The above derives from the definition of an "exclusive disjunction" {either one, but not both}.

提交回复
热议问题