# Hit and freeze help \[javascript\]

**URL:** <https://discourse.codecombat.com/t/hit-and-freeze-help-javascript/10783>\
**Category:** Uncategorized\
**Created:** [March 2, 2017, 8:45pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-javascript/10783 "2017-03-02T20:45:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![George\_Huchins\_Hun](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@George\_Huchins\_Hun](https://discourse.codecombat.com/u/George_Huchins_Hun)\
**Post date:** [March 2, 2017, 8:45pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-javascript/10783/1 "2017-03-02T20:45:52Z")

</div>

this is my code

```python
// You are trapped. Don't move, it'll be painful.

// This function checks if the enemy is in your attack range.
function inAttackRange(enemy) {
    var distance = hero.distanceTo(enemy);
    // Almost all swords have attack range of 3.
    if (distance <= 3) {
        return true;
    } else {
        return false;
    }
}

// Attack ogres only when they're within reach.
while (true) {
    // Find the nearest enemy and store it in a variable.
    var enemy = hero.findNearestEnemy();
    
    // Call inAttackRange(enemy), with the enemy as the argument
    // and save the result in the variable canAttack.
    inAttackRange();
    // If the result stored in canAttack is true, then attack!
    if (true) {
        hero.attack(enemy);

```

 ![](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/6/63ef2bc5b14401b3b309603714d7f00d49b342df.png)

this is what happens when i try to run it

---

<div class="post-metadata">

**Author:** ![juraj\_pechac](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/juraj_pechac/32/4987_2.png) [@juraj\_pechac](https://discourse.codecombat.com/u/juraj_pechac)\
**Post date:** [March 2, 2017, 8:48pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-javascript/10783/2 "2017-03-02T20:48:13Z")

</div>

Call inAttackRange( **enemy** ), with the enemy as the argument.

---

<div class="post-metadata">

**Author:** ![George\_Huchins\_Hun](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@George\_Huchins\_Hun](https://discourse.codecombat.com/u/George_Huchins_Hun)\
**Post date:** [March 2, 2017, 8:48pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-javascript/10783/3 "2017-03-02T20:48:35Z")

</div>

ok thank you i will try that

---

<div class="post-metadata">

**Author:** ![George\_Huchins\_Hun](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@George\_Huchins\_Hun](https://discourse.codecombat.com/u/George_Huchins_Hun)\
**Post date:** [March 2, 2017, 8:48pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-javascript/10783/4 "2017-03-02T20:48:59Z")

</div>

thank you soo much but my hero keeps running after the enemies why is this?

---

<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:** [March 2, 2017, 9:56pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-javascript/10783/5 "2017-03-02T21:56:07Z")

</div>

I think he will go after them until they are all dead…

---

<div class="post-metadata">

**Author:** ![George\_Huchins\_Hun](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@George\_Huchins\_Hun](https://discourse.codecombat.com/u/George_Huchins_Hun)\
**Post date:** [March 2, 2017, 9:57pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-javascript/10783/6 "2017-03-02T21:57:37Z")

</div>

can you help me with logical path because im having trouble with that now i have it up just look up logical path javascript

---

<div class="post-metadata">

**Author:** ![juraj\_pechac](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/juraj_pechac/32/4987_2.png) [@juraj\_pechac](https://discourse.codecombat.com/u/juraj_pechac)\
**Post date:** [March 3, 2017, 4:58am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-javascript/10783/7 "2017-03-03T04:58:41Z")

</div>

Call inAttackRange( **enemy** ), with the enemy as the argument  
and **save the result** in the variable **canAttack**.
