Help in munchkin swarm!

I am having trouble with my code, it is only attacking the enemy not the chest. My code is so far (with the problem):

loop {
    enemy = this.findNearestEnemy();
    var distance = this.distanceTo("Chest");
     if (distance < 10) {
          if ( this.isReady("cleave")) {
        this.cleave(enemy);
    } else {
        this.attack(enemy);
    }
    if (enemy) {
        this.attack("Chest");
   
    }  
    }
    
    // Check the distance to the nearest enemy.
    // If it comes closer than 10 meters, cleave it!
    // Else, attack the "Chest" by name.
    
    
} 

you check distance to chest then attack enemy,
you check if enemy exists then attack chest

switch those around

if enemy attack enemy
if distance attack chest

edit:
I just read the base comments, you should be checking distance to enemy not to chest, so:

if distanceTo enemy attack enemy
ELSE attack chest

i do not get :confused:

Merged Doublepost

i do not get it at all

loop {
    enemy = this.findNearestEnemy();
    var distance = this.distanceTo("Chest");                 // That is not enemy
    if (distance < 10) {
        if ( this.isReady("cleave")) {
            this.cleave(enemy);
        } else {
            this.attack(enemy);
        }
        if (enemy) {                                        // This is not chest
            this.attack("Chest");
        }
    }
    
    // Check the distance to the nearest enemy.            // as per this comment
    // If it comes closer than 10 meters, cleave it!
    // Else, attack the "Chest" by name. 
}

Ok, I’m with JFBM ugly indent makes ugly code (heck, I don’t like the so called one-true-brace) (I love python indenting, what is in is in and nothing hanging where it shouldn’t be)

Wow, now that I fixed the indent I see that the if enemy – atack chest is inside the if chest < 10 away.