This is how I checkout to see if a number is in a range (in between two other numbers):
var a = 10,
b = 30,
x = 15,
y = 35;
x < Math.max(a,b) && x > Math.min(a,b) // -> true
y < Math.max(a,b) && y > Math.min(a,b) // -> false
I have to do this math in my code a lot and I'm looking for shorter equivalent code.
This is a shorter version I came up with. But I am sure it can get much shorter:
a < x && x < b
true
a < y && y < b
false
But downside is I have to repeat x
or y
a<x&x<b
will return1
or0
, and is 7 characters shorter. \$\endgroup\$within(a,b)
orinrange(a,b)
somewhere in your code and using that. It's instantly obvious what it does and therefore easier to maintain in the future. \$\endgroup\$a=20; b=10; x=15
) \$\endgroup\$a
is supposed to be the minimum whileb
is max. Your example shows the minimum higher than the maximum! \$\endgroup\$