[SOLVED] Wishing well: Hero, Y U no Collect?

Ok… so I am on the wishing well level, and my code seems fine, the hero says “there is 104 gold”, but he takes 2 steps and just stops. Does not collect gold. Am I doing something wrong? Here is my code:
(Don’t think this is a bug, but I could be wrong)


//You need exactly 104 gold. 

    var less = "Nimis";
   var more = "Non satis";
   var requiredGold = 104;
   var item = hero.findNearestItem();

// This function calculates the sum of all coin values.
function sumCoinValues(coins) {
var coinIndex = 0;
var totalValue = 0;
// Iterate all coins.
while (coinIndex < coins.length) {
totalValue += coins[coinIndex].value;
coinIndex++;
}
return totalValue;
}

function collectAllCoins() {
var item = hero.findNearest(hero.findItems());
while (item) {
hero.moveXY(item.pos.x, item.pos.y);
item = hero.findNearest(hero.findItems());
}
}

       while (true) {
    var items = hero.findItems();
    // Get the total value of coins.
    var goldAmount = sumCoinValues(items);
    // If there are coins, then goldAmount isn't zero.
    if (goldAmount !== 0) {
        // If goldAmount is less than requiredGold
        // Then say "Non satis".
        if (goldAmount < requiredGold) {
            hero.say("Non satis");
        }
     
        // If goldAmount is greater than requiredGold
        // Then say "Nimis".
        if (goldAmount > requiredGold) {
            hero.say("Nimis");
        }
        // If goldAmount is exactly equal to requiredGold
        // If there is exactly 104 gold, then collect all coins.
        if (goldAmount == requiredGold) {
            hero.moveXY(item.pos.x, item.pos.y);
        }
    }
}

`
2 Likes

Could you please format it with the ` so it is easier to read?

2 Likes

please format according to the FAQ.
https://discourse.codecombat.com/guidelines

1 Like

YAS!!! I formatted properly :laughing:

1 Like

Hope dat is better :slight_smile:

1 Like

once you pickup your first coin you no longer have requiredGold

1 Like

You need to redefine the variable item. You defined the it before the while loop and because of that, it thinks that the old nearest item is still the nearest one. However, your hero says Non satis or Nimis, lightning will clear the field with a fresh field of coins. However, the variable item still references and old coin, so the system fails trying to look for that coin. Also, by not looping the moveXY, whenever the hero takes a coin, there will no longer be 104 coins and as a result, the coin field will refresh. A simple solution is to put it inside the while (true) loop:

while (true) {
var items = hero.findItems();
var goldAmount = sumCoinValues(items);
if (goldAmount !== 0) {
    // If goldAmount is less than requiredGold
    // Then say "Non satis".
    if (goldAmount < requiredGold) {
        hero.say("Non satis");
    }

    // If goldAmount is greater than requiredGold
    // Then say "Nimis".
    if (goldAmount > requiredGold) {
        hero.say("Nimis");
    }
    // If goldAmount is exactly equal to requiredGold
    // If there is exactly 104 gold, then collect all coins.
    if (goldAmount == requiredGold) {
******** (Remove these stars later)
        while(true){
          var item = hero.findNearestItem();
          hero.moveXY(item.pos.x, item.pos.y);
********
        }
    }
}
2 Likes

:open_mouth: Oh my mayonnaise! I cannot believe I missed that! Thank you VERY much :smiley:

3 Likes