Johny Bravo
Johny Bravo

Reputation: 424

Compare two numeric range in javascript

I have two number range. Lets say 1st rage is 6-9 and second is 1-15

I need to check that if it is conflicting. I mean if 1-15 is crossing 6-9, Valid ranges are 1-5, 10-15 but 1-15, 2-18 like this should return me that it is violating 6-9.

Currently I am checking only signle digit if it falls between range,

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

But now I need to check range. Any help is appreciated

Upvotes: 4

Views: 3043

Answers (3)

Derek
Derek

Reputation: 4751

If you want a pure function, this should suffice:

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

n1 and n2 are the first range, r1 and r2 are the second range, and boundaries indicate allowing overlaps on the boundary.

On the Array prototype, where comp is the 2nd range array:

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];
  }
}

See this JSFiddle for a working example.

Upvotes: 1

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48751

You can simplify your range checking code.

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

The following code constructs a Range object with a method for checking if another range fits inside of it. There is an inclusive flag like the one used in the above code.

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

Upvotes: 0

Anupam
Anupam

Reputation: 8016

Making use of your function, I added a new function to compare ranges

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

Upvotes: 2

Related Questions