How To Xor Three Variables In Javascript?
Solution 1:
If these are boolean values, just filter and test that only one is true:
functionisValid(...args) {
return args.filter(v => v).length == 1;
}
Solution 2:
I was working on a similar solution in the prev question. This one works with as many booleans as you want.
.includes()
makes sure that true
is in the array.
and comparing the index and lastIndex tells you if it's the only true
.
This does assume that XOR is exactly one true among all booleans checked.
constxor = ( ...booleans ) => ( booleans.includes( true ) && booleans.indexOf( true ) === booleans.lastIndexOf( true ));
console.log( xor( ...[ true, true, true, true, true ]), false );
console.log( xor( ...[ false, false, false, false, false ]), false );
console.log( xor( ...[ true, true, false, false, false ]), false );
console.log( xor( ...[ false, true, false, false, false ]), true );
Solution 3:
You have to be careful in JavaScript because expressions that look like boolean expressions may not return boolean values. The "logical" operators &&
and ||
perform a boolean interpretation of their left-side and right-side operands, but the ultimate value is the actual value as it was before the boolean interpretation.
If you want a function that interprets its arguments as booleans, and returns a value indicating the result of an XOR, then it might be simplest to do that explicitly:
function isValid(a, b, c) {
return !!(+!!a ^ +!!b ^ +!!c);
}
That performs a "to boolean" operation (!!
) on each parameter, followed by a "to number" (+
). Then the three numbers are XORed with ^
, and the result converted back to boolean and returned.
The "to boolean" operation implicitly performs the same interpretation as
if (something)
does; that is, the value, whatever it happens to be, is determined to be either "truthy" or "falsy", and the result expressed as either true
or false
. Converting a boolean value to a number then yields either 0 or 1.
Solution 4:
If your arguments are guaranteed to be boolean, it's enough to sum them and compare the result with 1:
lethasOneTrue = (...values) => values.reduce((x, y) => x + y) === 1
For arbitrary values, add the Boolean
typecast (or !!
if feeling hacky)
lethasOneTrue = (...values) => values.reduce((x, y) => x + Boolean(y), 0) === 1
For the sake of completeness,
lethasOneFalse = (...values) => values.reduce((x, y) => x + y) === values.length - 1
Since you mention typescript in your edit:
let hasOneTrue = (values: [boolean]): boolean => values.reduce((x, y) => x + (y ? 1 : 0) , 0) === 1
Solution 5:
When using XOR with 3 variables you get a true
condition in 2 cases
1:when only one of the variables is true
2:when all the variables is true.
The condition where only one variable is true works for 2 variables. So the solution to your query would be.
functionxor(a,b,c){
if(a && b && c){
returnfalse//You have to make an exception for all conditions being true
}
return ((a && !b && !c) || (!a && b && !c) || (!a && !b && c) || (a && b && c))
}
console.log(xor(true,true,true)) //falseconsole.log(xor(true,true,false)) //falseconsole.log(xor(true,false,false)) //true
Post a Comment for "How To Xor Three Variables In Javascript?"