# Mad Maxer hero stays still Javascript

**URL:** <https://discourse.codecombat.com/t/mad-maxer-hero-stays-still-javascript/27162>\
**Category:** Level Help\
**Created:** [January 30, 2021, 2:53pm UTC](https://discourse.codecombat.com/t/mad-maxer-hero-stays-still-javascript/27162 "2021-01-30T14:53:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saadval](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/saadval/32/19698_2.png) [@Saadval](https://discourse.codecombat.com/u/Saadval)\
**Post date:** [January 30, 2021, 2:53pm UTC](https://discourse.codecombat.com/t/mad-maxer-hero-stays-still-javascript/27162/1 "2021-01-30T14:53:29Z")

</div>

Here is my Code.

// Attack the enemy that’s farthest away first.

while(true) {  
var farthest = null;  
var maxDistance = 0;  
var enemyIndex = 0;  
var enemies = hero.findEnemies();

```
// Look at all the enemies to figure out which one is farthest away.
while (enemyIndex < enemies.length) {
    var target = enemies[enemyIndex];
    enemyIndex += 1;

    // Is this enemy farther than the farthest we've seen so far?
    var distance = hero.distanceTo(target);
    if (distance > maxDistance) {
        maxDistance = distance;
        farthest = target;
    }
}

if (farthest) {
    // Take out the farthest enemy!
    // Keep attacking the enemy while its health is greater than 0.
    while(health > 0) {
        var enemy = hero.findFarthestEnemy();
        hero.attack(enemy);
}

```

}  
}  
It doesn’t work Plz Help.

---

<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:** [January 30, 2021, 3:53pm UTC](https://discourse.codecombat.com/t/mad-maxer-hero-stays-still-javascript/27162/2 "2021-01-30T15:53:32Z")

</div>

Hey @Saadval! Welcome to Codecombat discourse! We have a lot of fun around here coding and talking about different topics. Before you continue, please check out our rules and guidelines so that you can become more familiar with how things go around here :).

Now with that being said could you please format your code properly.

You would do that by putting two of these:

`

At the beginning and end of your code so this:

while(true) {  
var farthest = null;  
var maxDistance = 0;  
var enemyIndex = 0;  
var enemies = hero.findEnemies();

Would look like this:  
`while(true) { var farthest = null; var maxDistance = 0; var enemyIndex = 0; var enemies = hero.findEnemies();`

Thanks!  
-@Luke10  
🐺🐺🐺

---

<div class="post-metadata">

**Author:** ![Saadval](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/saadval/32/19698_2.png) [@Saadval](https://discourse.codecombat.com/u/Saadval)\
**Post date:** [January 30, 2021, 4:12pm UTC](https://discourse.codecombat.com/t/mad-maxer-hero-stays-still-javascript/27162/3 "2021-01-30T16:12:23Z")

</div>

It still doesn’t work

---

<div class="post-metadata">

**Author:** ![Saadval](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/saadval/32/19698_2.png) [@Saadval](https://discourse.codecombat.com/u/Saadval)\
**Post date:** [January 30, 2021, 4:13pm UTC](https://discourse.codecombat.com/t/mad-maxer-hero-stays-still-javascript/27162/4 "2021-01-30T16:13:40Z")

</div>

and most of the problem is the “health” at the end said it wasn’t defined.

---

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.codecombat.com/u/abc)\
**Post date:** [January 30, 2021, 4:23pm UTC](https://discourse.codecombat.com/t/mad-maxer-hero-stays-still-javascript/27162/5 "2021-01-30T16:23:28Z")

</div>

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review [this topic](https://discourse.codecombat.com/t/welcome-to-codecombat-discourse-forums/15601), which shows all essentials of this board! Thanks!

> [@Saadval](#):
>
> `while(health > 0)`

Change the health here to `farthest.health`

---

<div class="post-metadata">

**Author:** ![Saadval](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/saadval/32/19698_2.png) [@Saadval](https://discourse.codecombat.com/u/Saadval)\
**Post date:** [January 30, 2021, 4:34pm UTC](https://discourse.codecombat.com/t/mad-maxer-hero-stays-still-javascript/27162/6 "2021-01-30T16:34:46Z")

</div>

the hero moves but he dies to quickly any help?

---

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.codecombat.com/u/abc)\
**Post date:** [January 30, 2021, 4:36pm UTC](https://discourse.codecombat.com/t/mad-maxer-hero-stays-still-javascript/27162/7 "2021-01-30T16:36:03Z")

</div>

You will probably need more health if you are dying quickly. Or you can get a better sword, what is your equipment?

---

<div class="post-metadata">

**Author:** ![Saadval](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/saadval/32/19698_2.png) [@Saadval](https://discourse.codecombat.com/u/Saadval)\
**Post date:** [January 30, 2021, 4:40pm UTC](https://discourse.codecombat.com/t/mad-maxer-hero-stays-still-javascript/27162/8 "2021-01-30T16:40:47Z")

</div>

![screenshot-codecombat.com-2021.01.30-11_40_03](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/c/8/c873895037ac4f57eeeead3857b09041d3f22ec4.jpeg)

---

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.codecombat.com/u/abc)\
**Post date:** [January 30, 2021, 4:57pm UTC](https://discourse.codecombat.com/t/mad-maxer-hero-stays-still-javascript/27162/9 "2021-01-30T16:57:21Z")

</div>

Yeah, you will need a better sword. Save up for a good sword, how many gems do you have?

---

<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:** [January 31, 2021, 4:57am UTC](https://discourse.codecombat.com/t/mad-maxer-hero-stays-still-javascript/27162/10 "2021-01-31T04:57:37Z")

</div>

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