Skip to main content
added 292 characters in body
Source Link
T.J. Crowder
  • 1.1m
  • 201
  • 2k
  • 2k

This solution seems to work, but my question is... why?

It doesn't work for what you described (only one can be true, and one must be true); it fails in the true, true, true case:

function isValid(a, b, c) { return a !== b !== c; }

console.log(isValid(true, true, true)); // true, should be false

I should note that I don't think your rule is quite the same as XOR, since true XOR true XOR true is true (because true XOR true is false, and false XOR true is true).

a !== b !== c is evaluated like this:

  • a !== b is evaluated and yields a boolean result, either true if the condition is true or false if it's false. Call that r. In the true, true, true case, true !== true is false so r = false.
  • r !== c is evaluated and yields its boolean result. In the true, true, true case, false !== true is true.

As you can see from the above, it works in almost all XOR cases, but not when all the inputs are true. It also doesn't work for other examples. Probably the most common error people make is trying to see if a, b, and c all have the same value:

if (a === b === c) // WRONG (almost always)

Since that's ((a === b) === c) which is (some_boolean === c), it's almost always wrong (for instance, if a, b, and c contain anything but boolean values).

This solution seems to work, but my question is... why?

It doesn't work:

function isValid(a, b, c) { return a !== b !== c; }

console.log(isValid(true, true, true)); // true, should be false

a !== b !== c is evaluated like this:

  • a !== b is evaluated and yields a boolean result, either true if the condition is true or false if it's false. Call that r. In the true, true, true case, true !== true is false so r = false.
  • r !== c is evaluated and yields its boolean result. In the true, true, true case, false !== true is true.

As you can see from the above, it works in almost all XOR cases, but not when all the inputs are true. It also doesn't work for other examples. Probably the most common error people make is trying to see if a, b, and c all have the same value:

if (a === b === c) // WRONG (almost always)

Since that's ((a === b) === c) which is (some_boolean === c), it's almost always wrong (for instance, if a, b, and c contain anything but boolean values).

This solution seems to work, but my question is... why?

It doesn't work for what you described (only one can be true, and one must be true); it fails in the true, true, true case:

function isValid(a, b, c) { return a !== b !== c; }

console.log(isValid(true, true, true)); // true, should be false

I should note that I don't think your rule is quite the same as XOR, since true XOR true XOR true is true (because true XOR true is false, and false XOR true is true).

a !== b !== c is evaluated like this:

  • a !== b is evaluated and yields a boolean result, either true if the condition is true or false if it's false. Call that r. In the true, true, true case, true !== true is false so r = false.
  • r !== c is evaluated and yields its boolean result. In the true, true, true case, false !== true is true.

As you can see from the above, it works in almost all XOR cases, but not when all the inputs are true. It also doesn't work for other examples. Probably the most common error people make is trying to see if a, b, and c all have the same value:

if (a === b === c) // WRONG (almost always)

Since that's ((a === b) === c) which is (some_boolean === c), it's almost always wrong (for instance, if a, b, and c contain anything but boolean values).

added 136 characters in body
Source Link
T.J. Crowder
  • 1.1m
  • 201
  • 2k
  • 2k

This solution seems to work, but my question is... why?

It doesn't work:

function isValid(a, b, c) { return a !== b !== c; }

console.log(isValid(true, true, true)); // true, should be false

a !== b !== c is evaluated like this:

  • a !== b is evaluated and yields a boolean result, either true if the condition is true or false if it's false. Call that r. In the true, true, true case, true !== true is false so r = false.
  • r !=== c is evaluated and yields its boolean result. In the true, true, true case, false !== true is true.

As you can see from the above, it works in almost all XOR cases, but not when all the inputs are true. It also doesn't work for other examples. Probably the most common error people make is trying to see if a, b, and c all have the same value:

if (a === b === c) // WRONG (almost always)

Since that's ((a === b) === c) which is (some_boolean === c), it's almost always wrong (for instance, if a, b, and c contain anything but boolean values).

This solution seems to work, but my question is... why?

It doesn't work:

function isValid(a, b, c) { return a !== b !== c; }

console.log(isValid(true, true, true)); // true, should be false

a !== b !== c is evaluated like this:

  • a !== b is evaluated and yields a boolean result, either true if the condition is true or false if it's false. Call that r.
  • r != c is evaluated and yields its boolean result.

As you can see from the above, it works in almost all XOR cases, but not when all the inputs are true. It also doesn't work for other examples. Probably the most common error people make is trying to see if a, b, and c all have the same value:

if (a === b === c) // WRONG (almost always)

Since that's ((a === b) === c) which is (some_boolean === c), it's almost always wrong (for instance, if a, b, and c contain anything but boolean values).

This solution seems to work, but my question is... why?

It doesn't work:

function isValid(a, b, c) { return a !== b !== c; }

console.log(isValid(true, true, true)); // true, should be false

a !== b !== c is evaluated like this:

  • a !== b is evaluated and yields a boolean result, either true if the condition is true or false if it's false. Call that r. In the true, true, true case, true !== true is false so r = false.
  • r !== c is evaluated and yields its boolean result. In the true, true, true case, false !== true is true.

As you can see from the above, it works in almost all XOR cases, but not when all the inputs are true. It also doesn't work for other examples. Probably the most common error people make is trying to see if a, b, and c all have the same value:

if (a === b === c) // WRONG (almost always)

Since that's ((a === b) === c) which is (some_boolean === c), it's almost always wrong (for instance, if a, b, and c contain anything but boolean values).

Source Link
T.J. Crowder
  • 1.1m
  • 201
  • 2k
  • 2k

This solution seems to work, but my question is... why?

It doesn't work:

function isValid(a, b, c) { return a !== b !== c; }

console.log(isValid(true, true, true)); // true, should be false

a !== b !== c is evaluated like this:

  • a !== b is evaluated and yields a boolean result, either true if the condition is true or false if it's false. Call that r.
  • r != c is evaluated and yields its boolean result.

As you can see from the above, it works in almost all XOR cases, but not when all the inputs are true. It also doesn't work for other examples. Probably the most common error people make is trying to see if a, b, and c all have the same value:

if (a === b === c) // WRONG (almost always)

Since that's ((a === b) === c) which is (some_boolean === c), it's almost always wrong (for instance, if a, b, and c contain anything but boolean values).