# Forest Miners - Javascript

**URL:** <https://discourse.codecombat.com/t/forest-miners-javascript/34894>\
**Category:** Level Help\
**Created:** [February 6, 2023, 11:20pm UTC](https://discourse.codecombat.com/t/forest-miners-javascript/34894 "2023-02-06T23:20:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jacob\_Thomas](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jacob_thomas/32/32737_2.png) [@Jacob\_Thomas](https://discourse.codecombat.com/u/Jacob_Thomas)\
**Post date:** [February 6, 2023, 11:20pm UTC](https://discourse.codecombat.com/t/forest-miners-javascript/34894/1 "2023-02-06T23:20:20Z")

</div>

My hero keeps on attacking a enemy on the side:

![Screenshot 2023-02-06 6.19.29 PM](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/1/e/1efa5a0101ce49906f61466ea537eb5c4a898df3.png)

My code:

```python
// Check if the mines are safe for the workers.
function checkEnemyOrSafe(target) {
    // If `target` (the parameter) exists:
    if (target) {
        hero.attack(target);
    } else {
        hero.say("All Clear!");
    } // Then attack target.
         // Otherwise:
         // Use say() to call the peasants.
}
while (true) {
    // Move to, and check the top right X mark.
    hero.moveXY(64, 54);
    var enemy1 = hero.findNearestEnemy();
    checkEnemyOrSafe(enemy1);
    // Move to the bottom left X mark.
    hero.moveXY(16, 14);
    // Save the result of findNearestEnemy() in a variable.
    var enemy2 = hero.findNearestEnemy();
    // Call checkEnemyOrSafe, and pass the
    // result of findNearestEnemy as the argument.
    checkEnemyOrSafe(enemy2);
}

```

---

<div class="post-metadata">

**Author:** ![Jason\_king\_lin](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@Jason\_king\_lin](https://discourse.codecombat.com/u/Jason_king_lin)\
**Post date:** [February 7, 2023, 6:14am UTC](https://discourse.codecombat.com/t/forest-miners-javascript/34894/2 "2023-02-07T06:14:19Z")

</div>

You have to check distance

---

<div class="post-metadata">

**Author:** ![MarmiteOnToast](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/marmiteontoast/32/46649_2.png) [@MarmiteOnToast](https://discourse.codecombat.com/u/MarmiteOnToast)\
**Post date:** [February 7, 2023, 10:52am UTC](https://discourse.codecombat.com/t/forest-miners-javascript/34894/3 "2023-02-07T10:52:50Z")

</div>

Try using glasses that don’t let you see through walls. If your hero can’t see the enemies on the other side of the trees, he won’t try to attack them.

-Marmite

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [February 25, 2023, 2:03pm UTC](https://discourse.codecombat.com/t/forest-miners-javascript/34894/4 "2023-02-25T14:03:39Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
