function findTheDistanceValue(arr1, arr2, d) {
function check(a) {
let i = arr2.findIndex
(num => num > a - d);
return i === -1 ||
arr2[i] > a + d;
}
arr2.sort((a, b) => a - b);
return arr1.reduce(
(count, a) =>
count + (check(a) ? 1 : 0), 0);
}
console.log(
"Distance: ", findTheDistanceValue(
[4, 5, 8], [10, 9, 1, 8], 2));
console.log(
"Distance: ", findTheDistanceValue(
[1, 4, 2, 3],
[-4, -3, 6, 10, 20, 30], 1));