Good afternoon, help me find the closest element on the page using javascript. Now I find the nearest element only in X. A bot whose 75 score will run upwards is because X is closer to it, but in fact a completely different element is closer on the page (the one that is to the right). Here is the code:
const closest = (a,g) => a.reduce((p,c) => Math.abs(cg) < Math.abs(pg) ? c : p); let botPositionX = parseInt(64); let botPositionY = parseInt(64); const data = [{x: 500, y: 285}, {x: 200, y: 324}, {x: 700, y: 200}]; const dataX = [500, 200, 700]; const dataY = [285, 324, 200]; const rightOrLeft = closest(dataX, botPositionX); const topOrBottom = closest(dataY, botPositionY); let found = data.find((loc) => { return loc.x === rightOrLeft; }); let coinPosX = parseInt(found.x); let coinPosY = parseInt(found.y); 