Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Problem with Mad Maxer sells out please help

Hi, I got problem with that level, this is my code. can anyone help me

while (true) {
    var closestGold = null;
    var minGoldDist = Infinity;
    var coinIndex = 0;
    var coins = hero.findItems();
    
    while (coinIndex < coins.length) {
      var  coin = coins[coinIndex];
    }
    if (coin) {
        distance = hero.distanceTo(coin);
    }
    if (distance < minGoldDist && coin.value == 3) {
        closestGold = coin;
        minGoldDist = distance;
    }
    coinIndex += 1;
}
if (closestGold) {
   
    hero.moveXY(closestGold.pos.x, closestGold.pos.y);
}

thx a lot for any help

I see a few problems with the code. First, your indentation needs some serious attention. Second, you have an errant closing bracket ‘}’ on line nine…it belongs elsewhere. Third, you’ve missed a step in defining ‘distance’.

I am sorry but I dont understand

The gaps at the start of each line aren’t right, for example inside the while loop the code should be four more spaces forward.

I hope this helps you understand,
Danny

hi, I changed code for this


while (true) {
    var closestGold = null;
    var minGoldDist = Infinity;
    var coinIndex = 0;
    var coins = hero.findItems();
}
while (coinIndex < coins.length) {
    var coin = coins[coinIndex];
    if (coin) {
        var distance = hero.distanceTo(coin);
        if (distance < minGoldDist && coin.value == 3) {
            closestGold = coin;
            distance = minGoldDist;
        }
        coinIndex += 1;
    }
    if (closestGold) {
        hero.moveXY(closestGold.pos.x, closestGold.pos.y);
    }
}

and it still doesnt work :(

I changed some " } " and now my hero takes two coins but the third one disappears

my code now looks like this:


while (true) {

    var closestGold = null;
    var minGoldDist = Infinity;
    var coinIndex = 0;
    var coins = hero.findItems();
    while (coinIndex < coins.length) {
        var coin = coins[coinIndex];
        if (coin) {
            var distance = hero.distanceTo(coin);
            if (distance < minGoldDist && coin.value == 3) {
                closestGold = coin;
                distance = minGoldDist;
            }
            coinIndex += 1;
        }
        if (closestGold) {
            hero.moveXY(closestGold.pos.x, closestGold.pos.y);
        }
    }
}
1 Like

Look at this line:

And the placement of this section:

When should you collect the coin? Before or after you’ve finished looking at all the coins?
That’s where your errors are,
Danny

Deadpool198 thx 4 Your help my problem is that I dont understand much what You write to me :frowning: I change my code and there is still a problem my hero collect only 2 coins and third, not , my code now looks like this:
please help, I passed this level on python but I wanna to learn how to do this in javascript :slight_smile:


while (true) {
    var closestGold = null;
    var minGoldDist = Infinity;
    var coinIndex = 0;
    var coins = hero.findItems();
    while (coinIndex < coins.length) {
        var coin = coins[coinIndex];
        if (coin) {
            var distance = hero.distanceTo(coin);
            if (distance < minGoldDist && coin.value == 3) {
                distance = minGoldDist;
            }
            closestGold = coin;
        }
        coinIndex += 1;
    }
    if (closestGold) {
        hero.moveXY(closestGold.pos.x, closestGold.pos.y);
    }
}

This bit is right now, well done:

This bit is wrong still:

You should put closestGold back inside the if statement, like you had it before.
Again the problem is here:

distance = minGoldDist; 

Are you sure that’s the right way round? Think about what distance is, that’s your distance to the coin, and you change it every loop.
Don’t you think you should define minGoldDist using distance instead? (switch the order around)
Danny

Finally got it, thx Deadpool198, I got one more question, why i should change(distance with minGoldDist) why it could be vice versa thx again for help

Think about it like this:
minGoldDistance is the variable you use to keep track of the closest coin.
distance is a variable you change every loop of the while loop which tells you how far away the coin is.
You want to update minGoldDistance with distance if distance is smaller than minGoldDistance so that minGoldDistance contains the smallest distance overall at the end. At the moment your redefining distance with the value of minGoldDistance.

var minGoldDistance = Infinity; // infinity
var distance = hero.distanceTo(coin); // something like 8
if (distance < minGoldDist && coin.value == 3) { // 8 is less than infinity
    closestGold = coin; // this is true
    distance = minGoldDist; // now distance has the value of "infinity" & minGoldDist is unchanged.
}

I hope this helps you understand,
Danny

I understand this now, thx a lot :slight_smile:

1 Like