Mad Maxer hero stays still Javascript

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.

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
:wolf::wolf::wolf:

It still doesn’t work

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

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, which shows all essentials of this board! Thanks!

Change the health here to farthest.health

the hero moves but he dies to quickly any help?

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

screenshot-codecombat.com-2021.01.30-11_40_03

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

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