Sand Snakes help needed JS[SOLVED]

Hi folks,
Can anyone tell me what I am doing wring in this code?
Tank you!

// This field is covered in firetraps.  Thankfully we've sent a scout ahead to find a path.  He left coins along the path so that if we always stick to the nearest coin, we'll avoid the traps.
// This canyon seems to interfere with your findNearest glasses!
// You'll need to find the nearest coins on your own.
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 coin's distance is less than the nearestDistance
        if (distance < nearestDistance) {
            // Set nearest to coin
            nearest = coin;
            nearestDistance = distance;    // Set nearestDistance to distance
        }
        coinIndex++;
    }
    if (nearest) {
        hero.moveXY(nearest.pos.x, nearest.pos.y);
    }    // If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
}

delete this and add it

before here

Thank you! But my hero still get killed

please post your code.

// This field is covered in firetraps.  Thankfully we've sent a scout ahead to find a path.  He left coins along the path so that if we always stick to the nearest coin, we'll avoid the traps.
// This canyon seems to interfere with your findNearest glasses!
// You'll need to find the nearest coins on your own.
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];
        coinIndex++;
        var distance = hero.distanceTo(coin);
        // If this coin's distance is less than the nearestDistance
        if (distance < nearestDistance) {
            // Set nearest to coin
            nearest = coin;
            nearestDistance = distance;    // Set nearestDistance to distance
        }
        
    }
    if (nearest) {
        hero.moveXY(nearest.pos.x, nearest.pos.y);
    }    // If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
}

you forgot to put var in front of these two lines

There’s no problem with your code. What equipment are you using?

Thank you for your answer!
Here is what I have.

Take off the mimic pet it fails the level

I just realised. Thank you!
I didn’t know that equipment are important.
f

remember to check the tickbox

Tickbox?
I do not follow you.

Nevermind you don’t have to all it does is closes the topic.