4

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

4
  • Are these ranges in form of array? Commented Dec 29, 2016 at 11:09
  • lets say the second range is a to b ... you need to check if a is within the test range OR b is within the test range OR ( a is below the test range AND b is above the test range) ... does that help? Commented Dec 29, 2016 at 11:12
  • @anu Yes ranges are in form of array. Commented Dec 29, 2016 at 11:25
  • @JaromandaX it should check that "a to g" is not cutting first range "b to e" Commented Dec 29, 2016 at 11:27

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

This work well but still there is an issue like for Range1 4 to 9 And Range2 6 to 8, it is failing
[4,9] and [6,8] fails
Hmm I was wondering if checking for both will solve the issue. range2.isRangeCrossed(range1,false) and range1.isRangeCrossed(range2,false) i.e. interchanging the target and source
Or update the same function isRangeCrossed to check for both ranges
1

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.

2 Comments

Don't understand the issue, but it always gives range overlapping, even when it is not. Can you please check again
e.g, [5,10] and [2,4] it says range overlaps
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.