[Question] Hold For Reinforcements JavaScript

Can someone explain to me why this code is not working??

Objective is:

Code given to start with is:

Here is my code:

while(true) {
    var flag = hero.findFlag();                // Set variable to find flag
    var enemy = hero.findNearestEnemy();       // Set variable to find enemy
    if (flag) {                                // If there is an enemy 
        hero.pickUpFlag(flag);                 // Pick up the flag.
    }
    else if (enemy && hero.isReady("cleave")) {  //Else if enemy and cleave is ready
        enemy = hero.findNearestEnemy();
    
        hero.cleave(enemy);                      // Cleave the enemy
    } else {                                     // Else 
       enemy = hero.findNearestEnemy();
       hero.attack(enemy);                       // Attack the enemy
    }
}

Here is the Error:

I was able to pass the level with this new code I wrote but it was simply because my armor allows me to tank all the damage and just cleave when its up. Pretty much the same code as above with out the else statement

while(true) {
    var flag = hero.findFlag();
    var enemy = hero.findNearestEnemy();
    if (flag) {
        // Pick up the flag.
        hero.pickUpFlag(flag);
    }
    else if (enemy && hero.isReady("cleave")) {
        enemy = hero.findNearestEnemy();
        // Else, attack!
        hero.cleave(enemy);
    }
}
    

Here is what my character does when I do pass. I feel like its cheating though since If I didn’t have all the best armor I would be mowed down probably.

Also this code will work as a second option. Basically just replaced attack with shield. But will shielding work and the attack won’t?

while(true) {
    var flag = hero.findFlag();                // Set variable to find flag
    var enemy = hero.findNearestEnemy();       // Set variable to find enemy
    if (flag) {                                // If there is an enemy 
        hero.pickUpFlag(flag);                 // Pick up the flag.
    }
    else if (enemy && hero.isReady("cleave")) {  //Else if enemy and cleave is ready
        enemy = hero.findNearestEnemy();
    
        hero.cleave(enemy);                      // Cleave the enemy
    } else {                                     // Else 
       enemy = hero.findNearestEnemy();
       hero.shield();                       // Attack the enemy
    }
}

Making a second else if statement solved the problem for my first solution but I am still confused as to why an else if statement works but an else does not

while(true) {
    var flag = hero.findFlag();                // Set variable to find flag
    var enemy = hero.findNearestEnemy();       // Set variable to find enemy
    if (flag) {                                // If there is an enemy 
        hero.pickUpFlag(flag);                 // Pick up the flag.
    }
    else if (enemy && hero.isReady("cleave")) {  //Else if enemy and cleave is ready
        enemy = hero.findNearestEnemy();
    
        hero.cleave(enemy);                      // Cleave the enemy
    } 
    else if (enemy) {                                     // Else 
       hero.attack(enemy);                       // Attack the enemy
    }
}

Is there really a difference? I thought If, Else If, Else statements worked like this

1. If (Do this first)
2. Else if (Do this)
3. Else (Do this)

I think the reason you are getting the error is that there isn’t always an enemy to attack, so if there are no flags, and cleave isn’t ready, then your character tries to attack the enemy. If there are no enemies alive you will get an error.
It works when you use hero.shield() because when you shield it doesn’t require a target. It also works when you have the else if because you are telling it to check if there is an enemy, if no enemy then it doesn’t run the attack code.
When you have an if/else statement it checks the if, then if that’s true it runs, or if its false it run the else statement. If you have any else if statements between those, it will check those in order to see if they are true before trying to run the else statement. It would also work if you changed it from an “else” statement to “if” statement. :slight_smile: