Mad maxer gets greedy - Javascript or any

Hello, could someone help me with this level please? I’m confused and I don’t know how to move forward.

javascript code:

// Collect more coins than your doppelganger.
// You only have a few seconds to collect coins. Choose your path wisely!
while(true) {
    var bestCoin = null;
    var maxRating = 0;
    var coinIndex = 0;
    var coins = hero.findItems();
    // Try calculating "value / distance" to decide which coins to get.
    while(coinIndex < coins.length) {
        var coin = coins[coinIndex];
        var distance = hero.distanceTo(coin);
        var bestCoin = coin.value / distance;
        if (bestCoin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }
    }
    coinIndex+=1;
}

I don’t think if (bestCoin) should be inside of your while loop.