Can someone help in hoarding gold? (Javascript)

Hello @Thomas_Olson.
Next time you do it, please could you post your code fully formatted (instructions here). For now I’ll do it so I can see it:

// Collect 25 gold, and then tell Naria the total.
// Use break to stop collecting when totalGold >= 25.

var totalGold = 0;
while(true) {
    var coin = hero.findNearestItem();
    if(coin) {
        // Pick up the coin.
        hero.move(coin.pos);
        // Add the coin’s value to totalGold.
        // Get its value with: coin.value
        totalGold += coin.Value;
    }
    if (totalGold >= 25) {
        // This breaks out of the loop to run code at the bottom.
        // The loop ends, code after the loop will run.
        break;
    }
}

// Done collecting gold!
hero.moveXY(58, 33);
// Go to Naria and say how much gold you collected.
hero.say("I have " + totalGold + " Gold");

There are two problems here:
one:

This is just a simple typo: it should be coin.value (lower case v).
two:

I’m not sure why you use move() here instead of moveXY() as I don’t think you will have learnt how to use it yet. Use moveXY() for now.
I hope this solves your problem.
Danny

1 Like