比较javascript中的两个数值范围

时间:2016-12-29 11:02:21

标签: javascript jquery

我有两个号码范围。 让我们说第一次愤怒是6-9,第二次是1-15

我需要检查一下它是否有冲突。我的意思是如果1-15越过6-9, 有效范围是 1-5, 10-15 但是这样的1-15,2-18会让我觉得它违反了6-9。

目前我只检查符号数字,如果它在范围之间,

if (typeof (Number.prototype.isBetween) === "undefined") {
    Number.prototype.isBetween = function (min, max, notBoundaries) {
        var between = false;
        if (notBoundaries) {
            if ((this < max) && (this > min)) between = true;
            alert('notBoundaries');
        } else {
            if ((this <= max) && (this >= min)) between = true;
            alert('Boundaries');
        }
        alert('here');
        return between;
    }
}

但现在我需要检查范围。任何帮助表示赞赏

3 个答案:

答案 0 :(得分:2)

利用你的功能,我添加了一个新功能来比较范围

if (typeof (Number.prototype.isBetween) === "undefined") {
    Number.prototype.isBetween = function (min, max, notBoundaries) {
        var between = false;
        if (notBoundaries) {
            if ((this < max) && (this > min)) between = true;
        } else {
            if ((this <= max) && (this >= min)) between = true;
        }
        return between;
    }
}

if (typeof (Array.prototype.isRangeCrossed) === "undefined") {
    Array.prototype.isRangeCrossed = function (target,notBoundaries) {
        var result = false;
        if ((target[0].isBetween(this[0],this[1],notBoundaries) ) || (target[1].isBetween(this[0],this[1],notBoundaries))){
          result = true;
        } 
        return result;
    }
}


var range1 = [6,9];
var range2 = [1,15];
var range3 = [2,5];
console.log(range2.isRangeCrossed(range1,false));
console.log(range3.isRangeCrossed(range1,false));

答案 1 :(得分:1)

如果你想要一个纯函数,这应该足够了:

var rangesOverlap = function(n1, n2, r1, r2, boundaries) {
    if (boundaries) {
        return n1 >= n2 || n2 >= r1 || r1 >= r2;
    } else {
        return n1 > n2 || n2 > r1 || r1 > r2;
    }
}

n1n2是第一个范围,r1r2是第二个范围,boundaries表示允许在边界上重叠。

Array原型上,comp是第二个范围数组:

Array.prototype.rangesOverlap = function(comp, boundaries) {
  if (boundaries) {
    return this[0] > this[1] || this[1] > comp[0] || comp[0] > comp[1];
  } else {
    return this[0] >= this[1] || this[1] >= comp[0] || comp[0] >= comp[1];
  }
}

有关工作示例,请参阅此JSFiddle

答案 2 :(得分:0)

您可以简化范围检查代码。

&#13;
&#13;
if (typeof(Number.prototype.isBetween) === 'undefined') {
  Number.prototype.isBetween = function(min, max, inclusive) {
    return inclusive ? (this <= max && this >= min) : (this < max && this > min);
  }
}

var range = { min : 7, max : 12 };
var validMinInclusive = range.min.isBetween(range.min, range.max, true);
var validMaxInclusive = range.max.isBetween(range.min, range.max, true);
var invalidMinExclusive = range.min.isBetween(range.min, range.max, false);
var invalidMaxExclusive = range.max.isBetween(range.min, range.max, false);

console.log(validMinInclusive, validMaxInclusive, invalidMinExclusive, invalidMaxExclusive);
&#13;
&#13;
&#13;

以下代码使用一种方法构造一个Range对象,该方法用于检查另一个范围是否适合其内部。有一个包含标志,就像上面代码中使用的那样。

&#13;
&#13;
function Range(min, max) {
  if (arguments.length === 0) {
    throw 'Range must include at least one value.';
  }
  if (arguments.length === 1) {
    max = min;
    min = 0;
  }
  this.min = min;
  this.max = max;
}
Range.inBounds = function(outer, inner, inclusive) {
  return inclusive
    ? (inner.min >= outer.min && inner.max <= outer.max)
    : (inner.min >  outer.min && inner.max <  outer.max);
};
Range.prototype.inBounds = function(target, inclusive) {
  return Range.inBounds(this, target, inclusive);
};

var rangeInner = new Range(6, 9);
var rangeOuter = new Range(1, 15);

var validRangeBounds   = rangeOuter.inBounds(rangeInner, false); // true
var invalidRangeBounds = rangeInner.inBounds(rangeOuter, false); // false

console.log(validRangeBounds, invalidRangeBounds);
&#13;
&#13;
&#13;