# Decoy Drill JavaScript Help \[SOLVED\]

**URL:** <https://discourse.codecombat.com/t/decoy-drill-javascript-help-solved/18470>\
**Category:** Level Help\
**Created:** [May 20, 2019, 11:41pm UTC](https://discourse.codecombat.com/t/decoy-drill-javascript-help-solved/18470 "2019-05-20T23:41:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![berry\_1](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/berry_1/32/10283_2.png) [@berry\_1](https://discourse.codecombat.com/u/berry_1)\
**Post date:** [May 20, 2019, 11:41pm UTC](https://discourse.codecombat.com/t/decoy-drill-javascript-help-solved/18470/1 "2019-05-20T23:41:33Z")

</div>

Here is my code:

```python
// 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.

---

<div class="post-metadata">

**Author:** ![berry\_1](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/berry_1/32/10283_2.png) [@berry\_1](https://discourse.codecombat.com/u/berry_1)\
**Post date:** [May 21, 2019, 12:16am UTC](https://discourse.codecombat.com/t/decoy-drill-javascript-help-solved/18470/2 "2019-05-21T00:16:06Z")

</div>

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:

 ![50%20PM](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/2/2a39327fccec723a2caafc93801b4f8e912d7fcf.jpeg)

I still don’t know how to fix this 😑

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [May 21, 2019, 12:43am UTC](https://discourse.codecombat.com/t/decoy-drill-javascript-help-solved/18470/3 "2019-05-21T00:43:08Z")

</div>

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;

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [May 21, 2019, 5:09pm UTC](https://discourse.codecombat.com/t/decoy-drill-javascript-help-solved/18470/4 "2019-05-21T17:09:09Z")

</div>

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:

```python
hero.attack(enemy)

```

This is what it would look like raw:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/4/40ebae1a767fc6c7d244788f041259c4220c5ccb.png)

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

> [@berry\_1](#):
>
> hero.buildXY(“decoy”, hero.pos.x - 5, hero.pos.y);  
> }

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

🦊🦊🦊  
-@Luke10

---

<div class="post-metadata">

**Author:** ![Shurutsue](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/shurutsue/32/10314_2.png) [@Shurutsue](https://discourse.codecombat.com/u/Shurutsue)\
**Post date:** [May 21, 2019, 7:26pm UTC](https://discourse.codecombat.com/t/decoy-drill-javascript-help-solved/18470/5 "2019-05-21T19:26:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![berry\_1](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/berry_1/32/10283_2.png) [@berry\_1](https://discourse.codecombat.com/u/berry_1)\
**Post date:** [May 21, 2019, 10:24pm UTC](https://discourse.codecombat.com/t/decoy-drill-javascript-help-solved/18470/6 "2019-05-21T22:24:28Z")

</div>

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:

 ![24%20PM](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/6/600f3833ab5e3e30a43f3243689a9d3ff874ef71.jpeg)

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 😟

---

<div class="post-metadata">

**Author:** ![berry\_1](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/berry_1/32/10283_2.png) [@berry\_1](https://discourse.codecombat.com/u/berry_1)\
**Post date:** [May 21, 2019, 10:27pm UTC](https://discourse.codecombat.com/t/decoy-drill-javascript-help-solved/18470/7 "2019-05-21T22:27:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Smith](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/smith/32/25681_2.png) [@Smith](https://discourse.codecombat.com/u/Smith)\
**Post date:** [October 12, 2021, 10:25am UTC](https://discourse.codecombat.com/t/decoy-drill-javascript-help-solved/18470/8 "2021-10-12T10:25:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Vanessa](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vanessa/32/33899_2.png) [@Vanessa](https://discourse.codecombat.com/u/Vanessa)\
**Post date:** [October 12, 2021, 1:46pm UTC](https://discourse.codecombat.com/t/decoy-drill-javascript-help-solved/18470/9 "2021-10-12T13:46:03Z")

</div>

Solutions are not allowed on the discourse

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [October 12, 2021, 6:27pm UTC](https://discourse.codecombat.com/t/decoy-drill-javascript-help-solved/18470/10 "2021-10-12T18:27:55Z")

</div>

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](https://discourse.codecombat.com/t/essentials-how-to-post-format-your-code-correctly/15521?u)), or just hang out with other coders. But before you proceed, please check out our guidelines: [this topic.](https://discourse.codecombat.com/t/this-topic/15601)  
Have a great time! 🥳

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 🙂
