Consider a sorted array containing numbers, for instance:
const array = [100, 400, 700, 1000, 1300, 1600]
We have a function that requires two arguments as input:
function foobar(min, max) {}
The task of this function is to retrieve the numbers from the array, starting from the first value that is >=
to the min
and ending with the last value that is >=
to the max
.
foobar(250, 1010) // returns [400, 700, 1000, 1300]
foobar(0, 15) // returns [100]
How can we achieve this using modern JavaScript?
array.filter((num) => {
return num >= min && num <= max
})
This solution always excludes the last number. 🤔