Sand Snakes Problem [JavaScript]

Unsure how to solve Sand Snakes(CS4, JavaScript), what am I doing incorrectly?

while(true) {
    var coins = hero.findItems();
    var coinIndex = 0;
    var nearest = null;
    var nearestDistance = 9999;
    // Loop through all the coins to find the nearest one.
    while(coinIndex < coins.length) {
        var coin = coins[coinIndex];
        var distance = hero.distanceTo(coin);
        // If this coins distance is less than the nearestDistance
        if (distance < nearestDistance)
            // Set nearest to coin
            var nearest = coin;
            // Set nearestDistance to distance
            var nearestDistance = distance;
    // If theres a nearest coin, move to its position. Youll need moveXY so you dont cut corners and hit a trap.
    hero.moveXY(coin.pos.x, coin.pos.y);


The problem is here:

    hero.moveXY(coin.pos.x, coin.pos.y);

What is ‘var nearest’ doing for you?