Decoy Drill JavaScript Help [SOLVED]

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!

4 Likes

Hi, I have had the same problem, but I have found the solution. I know it’s too late, but maybe it will help someone else.
The code is perfect, the bug is in misplacing “decoysBuilt += 1” outside the “if” of the lure build. Before this, the hero would add each coin collected and when it got to the fourth one it would end the process, but when I changed this, the process was done properly.

Solutions are not allowed on the discourse

Welcome to the forum! This is a family-friendly place where coders can share bugs, ask for help on any CodeCombat level (don’t forget to post your code correctly), or just hang out with other coders. But before you proceed, please check out our guidelines: this topic.
Have a great time! :partying_face:

It’s not a solution. He’s helping. Though Smith, since you’re not specifically replying to someone, you’re reviving the topic. Try not to do that :slightly_smiling_face:

1 Like