JSFiddle - React, Tailwind, and code Playground
JavaScript
function myFuzzyComparator(baseValue, range) {
let min = baseValue - range,
max = baseValue + range
return function(x){
return x > min && x < max
}
}
// test:
let testValues = new Array(100)
.fill(0)
.map(v => Math.round(Math.random() * 200))
let cmp1 = new myFuzzyComparator(175, 5)
let cmp2 = new myFuzzyComparator(100, 20)
let near175 = testValues.filter(cmp1)
let near100 = testValues.filter(cmp2)
document.body.innerHTML += '<h3>175 ± 5</h3>' + near175.toString() +
'<h3>100 ± 20</h3>' + near100.toString()