# \[SOLVED\]Code error? on hit and freeze... JavaScript

**URL:** <https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615>\
**Category:** Level Help\
**Created:** [April 11, 2022, 4:32pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615 "2022-04-11T16:32:27Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeeLand\_Teeslink](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/leeland_teeslink/32/11798_2.png) [@LeeLand\_Teeslink](https://discourse.codecombat.com/u/LeeLand_Teeslink)\
**Post date:** [April 11, 2022, 4:32pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/1 "2022-04-11T16:32:27Z")

</div>

```python

function inAttackRange(enemy) {
    var distance = hero.distanceTo(enemy);
  
    if (distance <= 3) {
        return true;
    } else {
        return false;
    }
}

while (true) {
    var enemy = hero.findNearestEnemy();
   
    var canAttack = inAttackRange(enemy)
     if canAttack = True: {
   
    hero.attack(enemy);
     }
}

```

could someone tell me what is wrong with my if can attack loop?  
Thanks!

---

<div class="post-metadata">

**Author:** ![LeeLand\_Teeslink](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/leeland_teeslink/32/11798_2.png) [@LeeLand\_Teeslink](https://discourse.codecombat.com/u/LeeLand_Teeslink)\
**Post date:** [April 11, 2022, 4:33pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/2 "2022-04-11T16:33:43Z")

</div>

the error it is giving me is  
 ![Screenshot 2022-04-11 11.33.20 AM](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/b/a/ba8cee0fbfa97374bc214466ca61df975e264f75.png)

---

<div class="post-metadata">

**Author:** ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)\
**Post date:** [April 11, 2022, 5:26pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/3 "2022-04-11T17:26:24Z")

</div>

> [@LeeLand\_Teeslink](#):
>
> `if canAttack = True:`

I believe this needs parenthesis like the first if-statement you made for the distance, and you should remove the colon at the end

---

<div class="post-metadata">

**Author:** ![LeeLand\_Teeslink](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/leeland_teeslink/32/11798_2.png) [@LeeLand\_Teeslink](https://discourse.codecombat.com/u/LeeLand_Teeslink)\
**Post date:** [April 11, 2022, 5:40pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/4 "2022-04-11T17:40:55Z")

</div>

yeah that’s not working same error

---

<div class="post-metadata">

**Author:** ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)\
**Post date:** [April 11, 2022, 5:41pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/5 "2022-04-11T17:41:30Z")

</div>

Can you post your new code please?

---

<div class="post-metadata">

**Author:** ![LeeLand\_Teeslink](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/leeland_teeslink/32/11798_2.png) [@LeeLand\_Teeslink](https://discourse.codecombat.com/u/LeeLand_Teeslink)\
**Post date:** [April 11, 2022, 5:42pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/6 "2022-04-11T17:42:51Z")

</div>

```python
   var canAttack = inAttackRange(enemy)
     if (canAttack) = True {
     
    // If the result stored in canAttack is true, then attack!
    hero.attack(enemy);
     }

```

---

<div class="post-metadata">

**Author:** ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)\
**Post date:** [April 11, 2022, 5:45pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/7 "2022-04-11T17:45:35Z")

</div>

I meant you got to put the whole thing in the parentheses.

How it should look like:

```python
if (condition) {
    #The code in here
}

```

There shouldn’t be anything outside the parentheses other than the word “if”

---

<div class="post-metadata">

**Author:** ![LeeLand\_Teeslink](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/leeland_teeslink/32/11798_2.png) [@LeeLand\_Teeslink](https://discourse.codecombat.com/u/LeeLand_Teeslink)\
**Post date:** [April 11, 2022, 5:49pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/8 "2022-04-11T17:49:15Z")

</div>

ohhhhhhhh, ok total missunderstanding

---

<div class="post-metadata">

**Author:** ![LeeLand\_Teeslink](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/leeland_teeslink/32/11798_2.png) [@LeeLand\_Teeslink](https://discourse.codecombat.com/u/LeeLand_Teeslink)\
**Post date:** [April 11, 2022, 5:52pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/9 "2022-04-11T17:52:31Z")

</div>

that fixed that part technicly but wait theres more

```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.
    var canAttack = inAttackRange(enemy);
     if (canAttack = True) {
     
    // If the result stored in canAttack is true, then attack!
    hero.attack(enemy);
     }
}

```

error is  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/f/9/f98e9b83797a5f81888ab5daa649674b4d22fea1.png)

---

<div class="post-metadata">

**Author:** ![moonwatcher348](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/moonwatcher348/32/24126_2.png) [@moonwatcher348](https://discourse.codecombat.com/u/moonwatcher348)\
**Post date:** [April 11, 2022, 6:05pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/10 "2022-04-11T18:05:16Z")

</div>

Unlike in Python where `True` is the representation, in Java and in many other languages, it’s `true`

---

<div class="post-metadata">

**Author:** ![LeeLand\_Teeslink](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/leeland_teeslink/32/11798_2.png) [@LeeLand\_Teeslink](https://discourse.codecombat.com/u/LeeLand_Teeslink)\
**Post date:** [April 11, 2022, 6:05pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/11 "2022-04-11T18:05:45Z")

</div>

i never knew that thank you

---

<div class="post-metadata">

**Author:** ![moonwatcher348](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/moonwatcher348/32/24126_2.png) [@moonwatcher348](https://discourse.codecombat.com/u/moonwatcher348)\
**Post date:** [April 11, 2022, 6:07pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/12 "2022-04-11T18:07:06Z")

</div>

And wait, one more thing, in most languages, if you want to check if 2 things are equivalent to each other, you need to use `==` and not `=`

---

<div class="post-metadata">

**Author:** ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)\
**Post date:** [April 11, 2022, 6:11pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/14 "2022-04-11T18:11:39Z")

</div>

Congrats on solving it, but please remove the solution as posting solutions is prohibited on the discourse because it will ruin the purpose of learning, thanks

---

<div class="post-metadata">

**Author:** ![LeeLand\_Teeslink](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/leeland_teeslink/32/11798_2.png) [@LeeLand\_Teeslink](https://discourse.codecombat.com/u/LeeLand_Teeslink)\
**Post date:** [April 11, 2022, 6:13pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/15 "2022-04-11T18:13:34Z")

</div>

i was unaware but thank you for telling me

---

<div class="post-metadata">

**Author:** ![moonwatcher348](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/moonwatcher348/32/24126_2.png) [@moonwatcher348](https://discourse.codecombat.com/u/moonwatcher348)\
**Post date:** [April 11, 2022, 6:14pm UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/16 "2022-04-11T18:14:10Z")

</div>

It only needed `==` not `===`, CodeCombat just wanted `===` because it’s better practice :] (and wait, ANOTHER thing, if you just wrote `if (canAttack) {` it would’ve also been fine)  
Also, just a side note, this is JavaScript and not Java, please be clear next time

---

<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:** [April 12, 2022, 6:14am UTC](https://discourse.codecombat.com/t/solved-code-error-on-hit-and-freeze-javascript/32615/17 "2022-04-12T06:14:40Z")

</div>

Closed on April 12, 2022, 6:14am UTC

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