# Can a?b<c:b>c be shortened in Javascript?

So I have the situation where I have one boolean value a and, if that one is true and b<c I want it to end up as true, if a is false and b>c I want it to resolve as true as well. If b=c I want it to be always false, no matter the value b and c are both numbers. My best approach so far is a?b<c:b>c or maybe b^c^a^b<c. Is there any shorter approach? I can’t stop thinking about it, and want confirmation this is the shortest possible way or find the solution...

• if a is true, does it matter what is output if b>c? same question for a being false and b<c Jun 24 at 4:39
• Are b and c guaranteed to be integers? Jun 24 at 6:35
• @Arnauld yes they are guaranteed integers Jun 24 at 17:52
• @thejonymyster no it doesn’t matter Jun 25 at 6:17

## Yes, it can be shorter!

c-b<a^a beats a?b<c:b>c by 2 bytes. My tests show that it works for any integer, including negatives. The downside to this formula is that a is repeated twice, which may be unideal depending on your situation.

• Very clever. This works for integers because 1) if a is false, c-b<a^a is true <=> c-b<a is true <=> b>c; 2) if a is true, c-b<a^a is true <=> c-b<a is false <=> c-b<1 is false <=> c-b>=1 is true <=> b<c. But it sometimes fails for non-integers. Jun 24 at 6:39
• I think c<b+a^a also works, again for integers.
– Neil
Jun 24 at 7:34
• What is precedence order for c-b<a^a? Is it ((c - b) < a) ^ a? Jun 26 at 16:04
• @EricDuminil Yes, you are correct Jul 2 at 17:59

# I don't think it can be any shorter, at least in the most general case

See dingledooper's 7-bytes solution that works for integers.

If the target expression were a?b<c:b>=c (note the change on the second comparison), the two conditions are exactly the opposite, and we can get away with comparing b and c only once with a^b>=c or !a^b<c.

But, the target is a?b<c:b>c, and since b<c and b>c cannot be inferred from each other, you either need to

• mention b and c twice each, to carry out two separate comparisons, or
• somehow use the three states (negative, zero, positive) of b-c.

For the first method, you already have the optimal answer. For the second, the best I can get is (c-b)*(a-.5)>0, 14 bytes (TIO).

Also note that b^c^a^b<c doesn't quite work.

• Javascript numbers are floating point, so have 4 states (negative, zero, positive, NaN). Similarly, the relation between a pair of numbers can be above, below, equal, or unordered (one or both NaN). It can still be interesting to golf the case of finite numbers, though, so you can exclude NaN inputs, and the case of inf-inf giving NaN. Jun 24 at 18:19