About of "mad-maxer-gets-greedy"

Hi I need your helps.
I think gold coins are required to clear on the priority.
How can I change for the better?
hint shows “value”. I don’t know how to use it.

// Collect more coins than your doppelganger.
// You only have a few seconds to collect coins. Choose your path wisely!
loop {
    var bestCoin = this.findItems("GOLD COIN");
    var maxRating = 0;
    var coinIndex = 0;
    var coins = this.findNearest(this.findItems());
    // Try calculating "value / distance" to decide which coins to get.
    if(coins){
        var distance = this.distanceTo(coins);
        if(distance < 20) {     
          pos = coins.pos;
          coinX = coins.pos.x;
          coinY = coins.pos.y;
          this.moveXY(coinX, coinY);
          pos = coins.pos;
        }else if(bestCoin){
          pos = coins.pos;
          coinX = coins.pos.x;
          coinY = coins.pos.y;
          this.moveXY(coinX, coinY);
          pos = coins.pos;
        }
    }
}

You want to rate each coin by coin.value / this.distanceTo(coin). The one with the highest rating should be the one you pick up each time. Comparing each rating to the maxRating and setting it to bestCoin if its rating is the new maxRating using Math.max is the technique we’re going for here, following the previous two Mad Maxer levels you did practicing that.

I’m having the same issue. I’m missing something small, and I know it. I just can’t think of it. Here is my code:

loop {
    var bestCoin = null;
    var maxRating = 0;
    var coinIndex = 0;
    var coins = this.findItems();
   
    while (coinIndex < coins.length) {
       var coin = coins[coinIndex];
       var distance = this.distanceTo(coin);
       var value = coin.value; 
       if (value / distance > maxRating) {
           maxRating = value / distance;
           bestCoin = coin; 
        }
    coinIndex += 1;
    }  
    if (bestCoin) {
        this.moveXY(bestCoin.pos.x, bestCoin.pos.y);
    }
}

What could I be missing?

Hello, Justin, and welcome. Please read the FAQ prior to your next post. You must learn how to format your code properly. I’ve done it for you this time, but please do so yourself in the future.

Your problem is that you only increment coinIndex outside the while-loop. This is quite irregular. Perhaps you meant to put it inside?

should i go back a bit,im having a ton of trouble with these mad maxers,or should i continue on forcing it through?

Reviewing previous levels can definitely help especially if you take the time to really understand all of the individual pieces that fit together. If you are still needing help, post your code and someone will walk you through it. Even if it is just explaining the code in more detail.

1 Like