I was playing the Javascript game with somebody and we were having fun making ridiculous and absurd expressions to make our inputs get a particular output.
This little charming one
!a!=!!b^!!-!a||!+!a|!c
always seemed to return 1. I tried to reason it out, but I gave up after losing track of all the !s.
Are there any values for a, b, and c which do not return 1? If not, why does it always return 1?
Short answer, yes.
a = false, b = false, c = trueis a counter-example because your equation is identical to(!!a || !!b || !c).Long answer:
is
which reduces to
so all of
a,bandcare only dealt with as truthy/falsey values and the result must be a1or0since|and^both coerce booleans to numbers.So obviously (from inspection of the right of the
||) if eitherais truthy orcis falsey, you get1.If
ais falsey andcis truthy, you have two possibilities,bis truthy in which case the^clause is1so the right of the||is never reached.bis falsey, in which case the^clause is0so the right of the||dominates to produce0.