Defend the garrison -js

I stack I don’t see what is wrong with my solution .Hero is quite strong the power of life is 2117 but it’s not enough

while (true) {
    var enemy = hero.findNearestEnemy();
    var flag = hero.findFlag();
    if (flag) {
        hero.pickUpFlag(flag);
    } else if (enemy) {
        if (hero.isReady("cleave")) {
            hero.cleave(enemy);
        } else if (hero.isReady("bash")) {
            hero.bash(enemy);
        } else {
            hero.attack(enemy);
        }
    } else {
        hero.attack(enemy);
    }
}

Could anyone help ?

That last hero.attack(enemy); means that if there is a flag, pick it up, else attack an enemy that may or may not exist. The first one is enough :slight_smile:

2 Likes

Hellenar is correct - adding to that is the fact that there is no check for an enemy before it so if there is a point that there is no flag and no enemy your code could fail.

1 Like

Thanks for your help. I erased that line and run the code again, Anya lived a little bit longer but not enough ,maybe I should buy for her different type of equipment . Maybe 2100 it’s less then she need

Can you post a screen shot of the equipment you are using?

Also, one of the things you’re supposed to use the flags for is to maneuver your hero to the left of the archers so that they attack anyone who comes through the gates and you take less damage. If you’re taking as much damage as you say, try staying out of the fight and letting the soldiers and archers do more. Use your flags so that you only step in and fight when when needed.

My equipment ,


I try wto fight that way.
Time out

Hi,

Not gonna lie was kinda proud first time beating this lvl. I had the same issue.

For me the trick was to only attack in case I planted a flag. You need to lure the enemies in front of the archers so you can deal with the backline casters. If you attack all enemies in sight you will indeed run out of time.

I also used multiple flag colours to make sure I could move without attacking anything

1 Like

Cool nice solution @zanox89! I just used ritic and the gift of the trees to beat the level

Mod edit: Please do not post solutions on this board. The purpose of the board is to help people write their own code and providing final solutions is counter productive to the learning process.