ChatGPT解决这个技术问题 Extra ChatGPT

Check if a value is within a range of numbers

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.

&& operator? ...

P
Pointy

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
}

Your coding is good but it doesn't fit the question, that's all. No offense. I always judge a solution by how good it answers the question. That's because I generally assume people know what they are asking for. The checkmark shows me this wasn't the case with this questioner.
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.
I like using the syntax like this: if (0.001 <= x && x <= 0.009) {...} seems a bit clearer to me :)
A
Alexander

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

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!
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).
I never claimed it to be whatever you imagined yourself it is. It is a viable option, nevertheless.
This is a little silly, it's not only probably slower but also much less obvious as to what the code does
K
Konstantin Grushetsky

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


H
Haroldo_OK

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

T
THE JOATMON

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);

F
Favour George

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 }


D
De Bonheur

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 ;

}

E
ElmirMahmudev

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).