Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

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 +;

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


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) {
  if( >= 25) {
this.moveXY(58, 33);
this.say("Done collecting gold!");

If you have an item giving you the 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( >= 25) break;
//some more stuff

(however you’ll hear some say that’s bad coding style)


this.say(“Done collecting gold!”);


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.