最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How would I compare a number to a range of numbers in Javascript? - Stack Overflow

programmeradmin0浏览0评论

I want to compare a range of numbers against a single number, basically like this in simple terms:

if X through Y equals Z, then do {}

What would be the proper way to do something like that in Javascript?

CLARIFICATION

I want to check if any number between X and Y is equal to Z

I want to compare a range of numbers against a single number, basically like this in simple terms:

if X through Y equals Z, then do {}

What would be the proper way to do something like that in Javascript?

CLARIFICATION

I want to check if any number between X and Y is equal to Z

Share Improve this question edited Dec 29, 2020 at 21:05 E_net4 30k13 gold badges111 silver badges151 bronze badges asked Jan 20, 2012 at 16:27 rshea0rshea0 12.2k9 gold badges30 silver badges41 bronze badges 8
  • 1 A range implies a loop. Show us the loop. – Jon Commented Jan 20, 2012 at 16:29
  • 1 So you're checking that X through Y are the same number as Z? So X and Y are indices of an array, or something? – user1106925 Commented Jan 20, 2012 at 16:30
  • I don't have a loop. This is being used for collision detection of 2 boxes. – rshea0 Commented Jan 20, 2012 at 16:30
  • You want to compare every number in the range against z? – Tristian Commented Jan 20, 2012 at 16:30
  • what does equals mean? x <= z <= y? – hvgotcodes Commented Jan 20, 2012 at 16:30
 |  Show 3 more comments

6 Answers 6

Reset to default 9

A range doesn't equal a number, do you mean that the number falls within the range?

if (1 <= Z && Z <= 25 ) {
    foo();
}

If you want to determine if Z is in the range [X, Y], then you can do:

if (X < Z && Z < Y) {
    // do stuff.
}

Otherwise, a range can't equal a number.

var arr = [...];

var compareRange = function(arr, x, y, z){

  for(var i=x; i<=y; i++){
    if(arr[i] != z){
      return false;
    }
  }

  return true;
};

To check if elements 1 through 3 are all 1:

var arr = [0, 1, 1, 1, 2];
var allOne = arr.slice(1, 4).filter(
    function(val) { return val !== 1; }).length === 0;

EDIT

Even better, thanks to @am not i am

var arr = [0, 1, 1, 1, 2];
var allOne = arr.slice(1, 4).every(function (val) { return val === 1; });

First the range function:

function range(a,b){
    var r = [a];
    for(var i=a;i<=b;i++){
         r.push(i);
    }
    return r;
}

Now use the function and make the comparison:

var a = range(1,6); // Range
var z = 7;
if(a.indexOf(z) > -1){
    // Collision !!!
}else{
   // No collision, I suppose...
}

I am not sure I have completely understood your question. I made a function that compares a number n to a range of numbers between min and max. This function returns an integer r from 1 to 5

  • n is less than min, r = 1
  • n is equal to min, r = 2
  • n is between min and max, r = 3
  • n is equal to max, r = 4
  • n is greater than max, r = 5
function compareToRange(n,min,max){
var r=((n<min)*1+(n==min)*2+((n>min)&&(n<max))*3+(n==max)*4+(n>max)*5);
}
发布评论

评论列表(0)

  1. 暂无评论