I have one array which is named as range and one lookpup variable. Now I am trying to get the matched range value from my range array. I tried to reduce method but it gives me an unexpected ouput.
Actual output
- My value is 51 then it returns me 100
Expected output
- Ex 1 - My value is 51 it's between 0-100 it should return 0 Ex 2 - My value is 135 it's between 100-150 it should return 100
My code
var lookup = 51;
var range = [0, 100, 150, 200, 300, 300.2];
let filtered = range.reduce((prev, curr) => Math.abs(curr - lookup) < Math.abs(prev - lookup) ? curr : prev);
console.log(filtered); //100
Not Working in this case
const lookup = 201;
const range = [200, 250, 300, 0, 450, 898].reverse();
const result = range.find(num => num <= lookup);
console.log(result); //0
0...450
if so then200...250
is included in that range. If that is the case then do you want the first range it matches, or the smallest range? Voting to close until you add clarity.