Created
May 5, 2020 18:17
-
-
Save dfkaye/5135495dd260cba71076cb8a8174f2ad to your computer and use it in GitHub Desktop.
XOR function in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 5 May 2020 | |
// XOR - because I keep forgetting what it means ("exclusive OR"). | |
// It compares 2 primitive values and returns true if only one is true, | |
// otherwise returns false. | |
// Could be more robust (reject Object comparisons, e.g.). | |
function XOR(a, b) { | |
return !a !== !b | |
} | |
/* test it out */ | |
var tests = [ | |
[1, 0], | |
[true, false], | |
[0, 0], | |
[1, -1] | |
].map(function(pair) { | |
return XOR.apply(0, pair); | |
}); | |
console.log(JSON.stringify(tests, null, 2)); | |
/* | |
[ | |
true, | |
true, | |
false, | |
false | |
] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment