Help javascript the one wizard ("witch" (get it?) is harder than magic exam. hmmm)

// Defeat as many ogres as you can.
// Use 'cast' and 'canCast' for spells.
hero.moveXY(4, 31);
var catapultsKilled = 0;
while(true) {
    var enemy = hero.findNearestEnemy();
    var catapult = hero.findNearest(hero.findByType("catapult"));
    if (catapult) {
        if (catapultsKilled === 0) {
        hero.moveXY(10, 37);
        }
        else {
            hero.cast("lightning-bolt", catapult);
        }
    }
    else if (enemy) {
        if (hero.canCast("chain-lightning")) {
            hero.cast("chain-lightning", enemy);
        }
        else if (hero.canCast("root") && enemy.health == 75){
            hero.cast("root", enemy);
        }
        else {
            hero.attack(enemy);
        }
    }
    else if (hero.health < hero.maxHealth) {
        hero.moveXY(5, 45);
        hero.cast("regen", hero);
    }
    else {
        hero.moveXY(5, 45);
    }
}

There you go. My code. @milton.jinich or @Deadpool198 help?

Can you show what happens and when your hero flees.

I’m killed by the scouts.

Show a screen shot please

oops, sorry
Edit:

You will need to have a good strategy for this level to get all the bonuses. Use chain-lightning, regen, lightning bolt and attacks for different types of ogres. @Dragonlouis, your code is a solution, just not for the final bonus.

oh, @abc? I’m going to point out that I got only the second bonus. not the third yet.