Can someone help in hoarding gold? (Javascript)


#1

// 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


#2

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


#3

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)


#4

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.