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

Backwoods Treasure - Javascript

#1

Hello! I really hate to have to post here - I’d wanted to be able to troubleshoot my code myself - but I’m quite stuck in trying to figure out what I’m doing wrong. I’m in the Backwoods Treasure Challenge level and I can’t seem to get my loop to break. Here’s a bit of the code I’m trying:

while (hero.gold <= 70) {
    hero.moveXY(58, 42);
    hero.moveXY(61, 54);
    hero.moveXY(73, 40);
    hero.moveXY(74, 43);
    hero.moveXY(74, 62);
    hero.moveXY(75, 59);
    hero.moveXY(69, 58);
    hero.moveXY(66, 64);
    hero.moveXY(49, 64);
    hero.moveXY(65, 43);
    hero.moveXY(62, 42);
    hero.moveXY(70, 60);
    hero.moveXY(44, 60);
    hero.moveXY(46, 52);
    hero.moveXY(51, 43);
    if (hero.gold >= 70) {
        break;
    }}
    hero.moveXY(25, 17);

… etc.

If you wouldn’t mind, could you tell me what is keeping that from breaking when hero.gold is greater than or equal to 70? When the character reaches 70, she finishes out the while loop and then she goes on to the next part. For her safety, I would like for her to stop and leave the loop as soon as she hits 70 coins. I’ve also tried == and ===, changing where I put the break statement, and changing to just < or >.

Thanks for any help you can provide!

1 Like
#2

Welcome to discourse @musicmaniac32!

Try changing that to a while (true) loop.

Try putting this outside the loop so it executes after breaking the loop.

#3

Hello @Chaboi_3000! Thank you for commenting and offering help! I just tried this:

while (true) {
    hero.moveXY(58, 42);
//lots of coordinates
    hero.moveXY(51, 43);
    if (hero.gold >= 70) {
        break;
    }
}
hero.move...

but she still finished the entire while statement before moving on. Am I still missing something?

#4

My tip for you is to try something like:

while (true) {
    var flag=hero.findFlags();
    if (hero.gold >= 70) {
        break;
    }
    else if(flag){
         // Try flags
    }
    
}
hero.move...
1 Like
#5

Thank you, @Chaboi_3000! I see what you’re getting at. I was hoping to just have a code that could run independent of my “flag” control, but at this point, I really just want to finish successfully at least once. :smile:

Thank you again!

#6

Glad it helped!

The code gets pretty ugly when you simultaneously try to move a specific place and check for the gold amount, so flags are the best option.

Enjoy your stay in the forum!

1 Like
#7

You need to maybe ask my brother this I’m good but i have to stay of of the codecombat because i’ts doing an up date but ask my brother juice_boxking if you search up juice it should pop up.