# Can someone help in hoarding gold? (Javascript)

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

loop {
var item=this.findNearestItem();
var coin = this.findNearestItem();
// Pick up the coin and add its value to the total.
// Get its value with: coin.value
if (item){
var pos = item.pos;
var x = pos.x;
var y = pos.y;
this.moveXY(x, y);
var totalGold = 0 + this.gold;
}

``````if (totalGold >= 25) {
// >= means totalGold is greater than or equal to 25.
// This breaks out of the loop to run code at the bottom.
break;
}
``````

}

this.moveXY(58, 33);
this.say(â€śDone collecting gold!â€ť);
// Go to Naria and say how much gold you collected.

I canâ€™t seem to pass it even though everything is done, am I missing something

nvm i changed what she said to I collected 25 gold and it worked

If i may suggest a more concise solution :

``````loop {
coin = this.findNearestItem()
if(coin) {
this.moveXY(coin.pos.x,coin.pos.y);
}
if(this.gold >= 25) {
break;
}
}
this.moveXY(58, 33);
this.say("Done collecting gold!");
``````

If you have an item giving you the this.gold attribute, you donâ€™t need to count your gold yourself and you can rely on this attribute
You are not using the coin variable in your code.
You can also do :

``````//some stuff
coin = this.findNearestItem()
if(coin) this.moveXY(coin.pos.x,coin.pos.y);
if(this.gold >= 25) break;
//some more stuff
``````

(however youâ€™ll hear some say thatâ€™s bad coding style)

this.say(â€śDone collecting gold!â€ť);

this.say(totalgold);

in my memory itâ€™s the point, because i stucked here for 2 days,and i changed this word in to totalgold.

i hope it helps.

Hi, Not getting it still after the tips. does anyone see my mistake??

// 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â€ť);

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

you would actually want to use: ` hero.moveXY(coin.pos.x, coin.pos.y);`