[SOLVED] I need help with Shine Getter, Javascript


#1

Here is my code:

// To grab the most gold quickly, just go after gold coins.

while(true) {
    var coins = hero.findItems();
    var coinIndex = 0;

    // Wrap this into a loop that iterates over all coins.
    while (coinIndex < coins.length) {
    var coin = coins[coinIndex];
    coinIndex = coinIndex + 1
    // Gold coins are worth 3.
    if (coin.value == 3) {
        // Only pick up gold coins.
        hero.moveXY(goldcoin.pos.x, goldcoin.pos.y);
        
    }  
    }
}

When I click on run my hero won’t do anything :confused:


#2

What is goldcoin? Why do you increment the index in the start and not in the end of the loop?


#3

I fixed the goldcoin, this is my code now

// To grab the most gold quickly, just go after gold coins.

while(true) {
    var coins = hero.findItems();
    var coinIndex = 0;

    // Wrap this into a loop that iterates over all coins.
    while (coinIndex < coins.length) {
    var coin = coins[coinIndex];
    // Gold coins are worth 3.
    if (coin.value == 3) {
        // Only pick up gold coins.
        hero.moveXY(coin.pos.x, coin.pos.y);
    }
    }
}


#4

my hero will just keep standing still and ive no idea why that is


#5

In the original code you misses semicolon to coinIndex = coinIndex + 1 In the correction you totally deleted that row. You need it, but at the end of the loop. You cannot exit a loop without some exit action and in your case
this is the incrementation.


#7

Oops, that’s better! Thanks a lot for helping me out :smile:


#8

At the end of the while (coinIndex < coins.length loop, not in the end of if coin.value condition.
Ok, you have done it yourself!
Delete your code in the last message, we don’t have to post whole solutions. ( You can keep the thanks :slight_smile: )