Help with Mad Maxer Code

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.
      var enemy= hero.findNearestEnemy();
       
        while (enemy.health>0) {
            hero.attack(enemy);
        }
    }
}

I don’t understand what I am doing wrong
My hero just keeps on attacking the decoys and not the ogres

Hi @Falcons118, welcome to the forum.
You seem to be missing some code at the top. Please could you post all of it. And if you put ```javascript at the start of your code it recognises the javascript comments, meaning the whole code isn’t red.
Thanks

Sorry about that… This is my first time posting something on this website

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;

        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.
      var enemy= hero.findNearestEnemy();
       
        while (enemy.health>0) {
            hero.attack(enemy);
        }
    }
}

@Deadpool198 is that better

@Deadpool198 can you help me now.

Inside the if farthest, what variable have you used to attack?

@Deadpool198 Isn’t the while loop supposed to be in there

You went through all the bother of determining ‘farthest’, but now you aren’t going to use it?

1 Like

@dedreous I don’t know how to use it. And when I click on the hints(in CodeCombat )there’s nothing useful that will help me in that level

Ok, let’s fix that then…I hope Danny won’t mind my stepping in here :wink:
Give me a few to take a closer look and I’ll repost my suggestions.

In your ‘if (farthest)’ code block, you are now re-defining ‘enemy’. You don’t need to do this, as ‘farthest’ is already defined as the enemy you want to take out first.

Attack farthest, not enemy.

1 Like

Additionally, the Hints are an excellent resource, but not always revealing. It is always a good idea to have a look at them, but then do not be afraid to ask for help if they were not enough.

It is a very narrow line between giving away too little and giving away to much. The goal here is to help folks learn, after all :slightly_smiling_face:

@dedreous Thanks for the help

1 Like

never mind it doesn’t work

My hero just says “but its dead”

Can you tell me what I need to change now @dedreous

Repost your code, as it is now…I’ll try to see if I can spot it.


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

    while (enemyIndex < enemies.length) {
        var target = enemies[enemyIndex];
        enemyIndex += 1;

        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(enemy.health>0) {
            hero.attack(farthest);
        }
    }
}

I changed the attack enemy to attack farthest

@dedreous isn’t that what you told me to change