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

Decoy Drill JavaScript Help

Here is my code:

// We are field testing a new battle unit: the decoy.
// Build 4 decoys, then report the total to Naria.

var decoysBuilt = 0;
while(true) {
    var coin = hero.findNearestItem();
    if(coin) {
        // Collect the coin!
        hero.moveXY(coin.pos.x, coin.pos.y);
    }
    // Each decoy costs 25 gold.
    // If hero.gold is greater than or equal to 25:
    if (hero.gold >= 25) {
        // buildXY a "decoy"
        hero.buildXY("decoy", hero.pos.x - 5, hero.pos.y);
    }
        // Add 1 to the decoysBuilt count.
        decoysBuilt += 1;
    
    if(decoysBuilt == 4) {
        // Break out of the loop when you have built 4.
        break;
        
    }
}

hero.say("Done building decoys!");
hero.moveXY(14, 36);
// Say how many decoys you built.
hero.say(decoysBuilt);

My hero doesn’t seem to be building any decoys, but moves to Naria and says, “4.” I’m not quite sure what’s wrong.

I changed my code a bit, but it’s still not working: my character will build decoys, but isn’t breaking out of the loop for some reason.

A screenshot:

I still don’t know how to fix this :expressionless:

Have you checked to see if your decoyBuilt variable is increasing?

Just to quickly check, add a hero.say(decoyBuilt); in the loop after you build a decoy and count it. That will let you see if you are adding to the variable like you think you are.

If that doesn’t help you find the issue, check the bigger hint.

Bigger Hint

The variable is not set up correctly to add one to the variable.
decoysBuilt += 1;

2 Likes

Just as a reminder, try to post your code according to our community guidlines! Basically all you need to do is put three ` before and after your code

This causes this piece of code:

hero.attack(enemy)

To become:

hero.attack(enemy)

This is what it would look like raw:

Now about your issue correct me if I am wrong but on this part:

I don’t believe you put in the coordinate for y.
Also on x you didn’t specify the exact coordinates.

:fox_face::fox_face::fox_face:
-@Luke10

1 Like

see brooksy125’s answer further up and in addition to that:
Think about when to actually increase this variable.
As it stands now, I believe that if you would fix it so that it actually increases,
it would stop after running the while loop 4 times, most likely after picking up 4 coins and/or building one decoy.

Hi all.
Thanks for the advice.
Unfortunately, despite all revisions, after I tried everything everyone said, my code still isn’t working. It’s back to the original error: not building decoys. Increasing the value of the decoysBuilt variable seems to cause my hero to not build any decoys.
Another screenshot:

No one has to respond, but if they did, that would be great. I’m struggling, and this is a homework assignment due last Friday, so I’d like to get it done as soon as possible :worried:

Hi again. Seriously, no one has to respond this time. I figured it out. Thanks for the help, though!

3 Likes