I have ranges described as string
let ranges = "0,71-140,34,142-216,20-30,7"
(not sorted; one number eg 34 means range 34-34).
- How to check that number
num
is in some range (of given ranges) - How to check that number is smaller than smallest range or bigger than biggest range?
This is inversion of this question.
const isInRanges = (ranges, num) => {
return false; // magic here
}
const isOutOfRanges = (ranges, num) => {
return false; // magic here
}
// ------------------------------------------------
// TESTS - we should always get TRUE in console
// ------------------------------------------------
let myRanges = "0,71-140,34,142-216,20-30,7";
// is in tests
let casesIn = [
[0, true],
[25, true],
[35, false],
[200, true],
[8, false]
];
for (const c of casesIn) {
console.log(c[0], isInRanges(myRanges, c[0]) == c[1])
}
// is out tests
let casesOut = [
[-2, true],
[60, false],
[300, true],
[7, false]
];
for (const c of casesOut) {
console.log(c[0], isOutOfRanges(myRanges, c[0]) == c[1])
}
Solution will be two functions (look on snippet) which returns true/false - and pass all test-cases (we should always see 'true' on the console).