My hero doesn't move fast enough (Diamond Dozen)

So the code itself works just fine but for some weird reason the coins despawn just as my hero is getting to them, so the level doesn’t complete.
Code:

function findMostHealth(enemies) {
    var target = null;
    var targetHealth = 0;
    var enemyIndex = 0;
    while(enemyIndex < enemies.length) {
        var enemy = enemies[enemyIndex];
        if(enemy.health > targetHealth) {
            target = enemy;
            targetHealth = enemy.health;
        }
        enemyIndex += 1;
    }
    return target;
}

function valueOverDistance(item) {
    return item.value / hero.distanceTo(item);
}

// Return the item with the highest valueOverDistance(item)
function findBestItem(items) {
    var bestItem = null;
    var bestValue = 0;
    var itemsIndex = 0;
    
    // Loop over the items array.
    // Find the item with the highest valueOverDistance()
    while (itemsIndex < items.length) {
        var item = items[itemsIndex];
        if (item.value > bestValue) {
            bestItem = item;
            bestValue = item.value;
        }
        itemsIndex++;
    }
    return bestItem;
}

while(true) {
    var enemies = hero.findEnemies();
    var enemy = findMostHealth(enemies);
    if(enemy && enemy.health > 15) {
        while(enemy.health > 0) {
            hero.attack(enemy);
        }
    } else {
        var coins = hero.findItems();
        var coin = null;
        coin = findBestItem(coins);
        if(coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }
    }
}

Gear:

Hi ScienceBoi,

These three lines:

Remember you’re trying to find the item with the best valueOverDistance, not just the one with the highest value. What do you need to change?

Jenny

Yeah. Is it more worth it to go over the whole map for a gold coin, or move a bit and get a sliver coin

Oh so that was why they had the valueOverDistance function.