I'd suggest going this route:
class IntervalList {
constructor() {
this.data = [];
}
add(low, high) {
if (low > high) {
[low, high] = [high, low];
}
for (let [x,y] of this.data) {
if (x < low) {
if (y > low) return false;
} else if (x > low) {
if (x < high) return false;
}
}
this.data.push([low, high]);
return true;
}
}
Then you say things like
let c = new IntervalList();
c.add(10, 20);
c.add(20, 30);
c.add(5,7);
c.add(3,5);
c.add(9,7)
Let the data structure maintain itself.
Note that what you want to do is tricky. Some people might erroneously think you that to check if (p,q) overlaps (r,s) you just have to check whether p is between r and s or q is between r and s. THIS IS NOT TRUE. Because if the values were arranged "p r s q" that algorithm would fail. Note how the above algorithm looks weird but it takes care of that case. That is why the problem is interesting.
[30, 40]allowed and not[5, 35]? I understand you want to get min and max of all the numbers in the array of arrays, and the pushed element must have values outside of min and max. Correct?