180

I want to check if a value is in an accepted range. If yes, to do something; otherwise, something else.

The range is 0.001-0.009. I know how to use multiple if to check this, but I want to know if there is any way to check it in a single if statement.

thumbtackthief
  • 6,093
  • 10
  • 41
  • 87
Sotiris
  • 38,986
  • 11
  • 53
  • 85

9 Answers9

332

You're asking a question about numeric comparisons, so regular expressions really have nothing to do with the issue. You don't need "multiple if" statements to do it, either:

if (x >= 0.001 && x <= 0.009) {
  // something
}

You could write yourself a "between()" function:

function between(x, min, max) {
  return x >= min && x <= max;
}
// ...
if (between(x, 0.001, 0.009)) {
  // something
}
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 34
    Well, I appreciate what you're saying, but I suggest that you may want to reconsider the premise that people here generally "know what they are asking for." That may be true in a very *narrow* sense, but often these questions reflect some bad design decision that should be revisited. I've been answering programming questions for about 30 years now, so I've seen it happen many times. I do of course prefer better questions, wherein the overall goal is clearly explained. Here, we really have no idea of the *reason* for the question, so we have no context. – Pointy Jun 23 '11 at 13:46
  • 24
    I like using the syntax like this: if (0.001 <= x && x <= 0.009) {...} seems a bit clearer to me :) – Omri Jul 24 '14 at 06:41
77

Here is an option with only a single comparison.

// return true if in range, otherwise false
function inRange(x, min, max) {
    return ((x-min)*(x-max) <= 0);
}

console.log(inRange(5, 1, 10));     // true
console.log(inRange(-5, 1, 10));    // false
console.log(inRange(20, 1, 10));    // false
Alexander
  • 3,965
  • 1
  • 12
  • 16
  • 4
    This is sweet it doesn't matter if min is lower or max is lower it will still work. In fact it might be better to call min, max something like start, end. great algo either way thanks! – James Harrington Jul 09 '18 at 17:28
  • 7
    Won't somebody PLEASE think of the time complexity!? I'd go with the accepted solution because two comparisons at Θ(2) is better than one comparison and three arithmetic operations at O(4). – Matt McCarthy Sep 06 '20 at 14:58
  • 2
    I never claimed it to be whatever you imagined yourself it is. It is a viable option, nevertheless. – Alexander Sep 07 '20 at 21:05
  • 3
    This is a little silly, it's not only probably slower but also much less obvious as to what the code does – adrian Dec 02 '20 at 06:04
22

If you must use a regexp (and really, you shouldn't!) this will work:

/^0\.00([1-8]\d*|90*)$/

should work, i.e.

  • ^ nothing before,
  • followed by 0.00 (nb: backslash escape for the . character)
  • followed by 1 through 8, and any number of additional digits
  • or 9, followed by any number of zeroes
  • $: followed by nothing else
Konstantin Grushetsky
  • 1,012
  • 1
  • 15
  • 32
Alnitak
  • 334,560
  • 70
  • 407
  • 495
18

If you're already using lodash, you could use the inRange() function: https://lodash.com/docs/4.17.15#inRange

_.inRange(3, 2, 4);
// => true

_.inRange(4, 8);
// => true

_.inRange(4, 2);
// => false

_.inRange(2, 2);
// => false

_.inRange(1.2, 2);
// => true

_.inRange(5.2, 4);
// => false

_.inRange(-3, -2, -6);
// => true
Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80
4

I like Pointy's between function so I wrote a similar one that worked well for my scenario.

/**
 * Checks if an integer is within ±x another integer.
 * @param {int} op - The integer in question
 * @param {int} target - The integer to compare to
 * @param {int} range - the range ±
 */
function nearInt(op, target, range) {
    return op < target + range && op > target - range;
}

so if you wanted to see if x was within ±10 of y:

var x = 100;
var y = 115;
nearInt(x,y,10) = false

I'm using it for detecting a long-press on mobile:

//make sure they haven't moved too much during long press.
if (!nearInt(Last.x,Start.x,5) || !nearInt(Last.y, Start.y,5)) clearTimeout(t);
4

If you want your code to pick a specific range of digits, be sure to use the && operator instead of the ||.

if (x >= 4 && x <= 9) {
  // do something
} else {
  // do something else
}

// be sure not to do this

if (x >= 4 || x <= 9) {
  // do something
} else {
  // do something else
}
Favour George
  • 1,525
  • 13
  • 18
4

You must want to determine the lower and upper bound before writing the condition

function between(value,first,last) {

 let lower = Math.min(first,last) , upper = Math.max(first,last);
 return value >= lower &&  value <= upper ;

}
De Bonheur
  • 742
  • 10
  • 13
2

const inRange = (num, num1, num2) => Math.min(num1, num2) <= num && Math.max(num1, num2) >= num;

Could be like this if you want to make inRange inclusive and not depend on order of range numbers (num1, num2).

1

Here's a short ES6 function:

const inRange = (num, min, max) => num >= min && num <= max
Ali Mousavi
  • 855
  • 7
  • 15