Hoarding Gold (No coin.value code?)

I’m having trouble getting my avatar to actually go around and collect the gold. She just runs straight to Naria and says “Done collecting gold!”. Any suggestions on why the program is ignoring my code?

var totalGold = 0;
loop {
    var coin = this.findNearestItem();
    // Pick up the coin and add its value to the total.
    // Get its value with:  coin.value
    if (coin) {
        var pos = coin.pos;
        var x = this.pos.x;
        var y = this.pos.y;
        this.moveXY(x,y);
        totalGold = totalGold += coin.value;
    }
    
    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(64, 34);
this.say("Done collecting gold!");
1 Like

I don’t know if this is the problem, but it is wrong…

it should either be “totalGold += coin.value;” or “totalGold = TotalGold + coin.value” not both.

2 Likes

Strangely enough we where trying “totalGold + coin.value = totalGold” and it wouldn’t work. It has to be " totalGold = totalGold + coin.value "

1 Like

An assignment always looks the same:

variable = computation

It is not forbidden to use variable in computation if it has already been defined. In fact, the following expression is absolutely valid:

variable = variable

Look at the following example:

totalGold = totalGold + coin.value

What actually happens is this:

  1. Get the value stored in totalGold
  2. Get the value stored in coin.value
  3. Add the two stored values together
  4. Store the result in a variable named totalGold, unimportant whether it existed before or not.
4 Likes

[…deleted poorly formatted, unhelpful code…]

1 Like

Draz, please do not give out correct code, as it gives any reader the answer without providing the learning. Besides, the person you are replying to gave the issue over five months ago. And you still haven’t learned to post your code correctly.

1 Like

Could you help me, I need to figure out how to pick up the “coin” I do not remember how to pick up items in this game without it being an “item”

1 Like

You pick the coin by moving to its position.

self.move(coin.pos)
#or
self.moveXY(coin.pos.x,coin.pos,y)
1 Like

the reason is that you have to say totalGold in the message

1 Like

Please do not revive dead threads! This problem has already been solved and left alone for one year.

1 Like

just trying to be helpful

1 Like

Thanks for trying to help the CodeCombat community, but this problem is already solved.

1 Like