Is there anyway to implement XOR in javascript

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

    Checkout this explanation of different implementations of XOR in javascript.

    Just to summarize a few of them right here:

    if( ( isEmptyString(firstStr) || isEmptyString(secondStr)) && !( isEmptyString(firstStr) && isEmptyString(secondStr)) ) {
       alert(SOME_VALIDATION_MSG); 
       return; 
    }
    

    OR

    if( isEmptyString(firstStr)? !isEmptyString(secondStr): isEmptyString(secondStr)) {
       alert(SOME_VALIDATION_MSG); 
       return;
    }
    

    OR

    if( (isEmptyString(firstStr) ? 1 : 0 ) ^ (isEmptyString(secondStr) ? 1 : 0 ) ) {
       alert(SOME_VALIDATION_MSG); 
       return;
    }
    

    OR

    if( !isEmptyString(firstStr)!= !isEmptyString(secondStr)) {
       alert(SOME_VALIDATION_MSG); 
       return;
    }
    

提交回复
热议问题