Find Nearest Point That Has the Same X or Y Coordinate LeetCode solution in JavaScript

1
var nearestValidPoint = function (x, y, points) {
2
let min = Infinity;
3
let idx = -1;
4
points.forEach(([a, b], i) => {
5
if (a === x || b === y) {
6
const dist = Math.abs(x - a) + Math.abs(y - b);
7
if (dist < min) {
8
idx = i;
9
min = dist;
10
}
11
}
12
});
13
return idx;
14
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0