Ok, can you explain the two values: that gives you the closest first value, the second one is ignored? And do you have a description of what youâ€™re trying to achieve and what the data structures look like?

What you want is probably a depth-first or breadth-first search (your choice), which is easiest to implement using a recursive function, but how you do that & what you need is easier to advise on with the above info.

Anyhoo, that code is fine for getting that closest path:

So you set what the closest node is (that could just be `let closest;`

but makes sense to set it as the first node, can assume there is always at least that). You set what the distance is to `Infinity`

, because everything is `<`

that â€“ means that the first check in the loop (`if (distance < closestDistance)`

) is always going to return true.

The `distance`

has to be `target - theCurrentValueYouAreComapring`

. And because you donâ€™t care what that current value is, just what the difference is between it and the target value, you use `abs`

to give you the absolute value (if itâ€™s negative, it strips the sign, so you always get a positive number). Then you can compare `distance`

to the `closestDistance`

. On the first run of the loop, thatâ€™s `Infinity`

, so `distance`

will always be less than that.

For reference I had something like:

```
function getClosestPath (nodes, target) {
let closestKey = nodes[0][0];
let closestTarget = 0;
let diff = Infinity;
for (const [k, [[x], [y]]] of nodes) {
const currDiffX = Math.abs(target - x);
const currDiffY = Math.abs(target - y);
if (currDiffX < diff) {
closestKey = k;
closestTarget = x;
diff = currDiffX;
}
if (currDiffX < diff) {
closestKey = k;
closestTarget = y;
diff = currDiffY;
}
console.log(`Closest node key is ${closestKey}, closest target in that node is ${closestTarget}`);
}
return `${closestKey} - ${closestTarget}`;
}
```

But not quite sure how that fits in now â€“ you probably donâ€™t want your `closestPath`

function in its current form, youâ€™ll need to integrate some of the logic of it into a [likely recursive] function that walks the tree