# Help on mind the trap (Javascript)

**URL:** <https://discourse.codecombat.com/t/help-on-mind-the-trap-javascript/29512>\
**Category:** Level Help\
**Created:** [July 2, 2021, 6:14pm UTC](https://discourse.codecombat.com/t/help-on-mind-the-trap-javascript/29512 "2021-07-02T18:14:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![blackknight3489](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@blackknight3489](https://discourse.codecombat.com/u/blackknight3489)\
**Post date:** [July 2, 2021, 6:14pm UTC](https://discourse.codecombat.com/t/help-on-mind-the-trap-javascript/29512/1 "2021-07-02T18:14:43Z")

</div>

When I run this code my hero walked straight to the bombs help please?

```python
// If you to attack a distant enemy, your hero will charge toward it, ignoring all flags.
// You'll need to make sure you only attack enemies who are close to you!

while(true) {
    var flag = hero.findFlag();
    var enemy = hero.findNearestEnemy();
    
    if(flag) {
        // Pick up the flag.
        hero.pickUpFlag(flag);
        hero.say("I should pick up the flag.");
    } else if(hero.distanceToEnemy < 10);
 {
        // Only attack if the enemy distance is < 10 meters
        hero.attack(enemy);
    }
}

```

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [July 7, 2021, 1:48pm UTC](https://discourse.codecombat.com/t/help-on-mind-the-trap-javascript/29512/2 "2021-07-07T13:48:26Z")

</div>

Hi blackknight3489,

Welcome to the forum (and sorry it’s taken so long for you to get a reply).

Have another look at this line:

```python
else if(hero.distanceToEnemy < 10);

```

What code have you used to find the distance to an enemy before?

Post again if you need more help.

Jenny

---

<div class="post-metadata">

**Author:** ![Matignacio](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/matignacio/32/23165_2.png) [@Matignacio](https://discourse.codecombat.com/u/Matignacio)\
**Post date:** [July 13, 2021, 2:37pm UTC](https://discourse.codecombat.com/t/help-on-mind-the-trap-javascript/29512/3 "2021-07-13T14:37:39Z")

</div>

> [@blackknight3489](#):
>
> ```python
> else if(hero.distanceToEnemy < 10);
> {
> // Only attack if the enemy distance is < 10 meters
> hero.attack(enemy);
> }
> }
> 
> ```

**In this part you must correct the code as follows:**

```python
} else if (hero.distanceToEnemy <= 10) {
     hero.attack(enemy);
}

```
