Restless Dead - javascript - army is dying

My soldiers and my hero cannot defeat the skeletons. here is my code.

// Kill the Yeti, then collect the coins
let redX = {x:19, y:40};
hero.moveXY(60, 25);
while (hero.pos.x !== redX.x) {
    let coins = hero.findItems();
    let enemy = hero.findNearestEnemy();
    if (enemy) {
        hero.attack(enemy);
    }
    if (coins) {
        for (let coin of coins) {
            hero.move(coin.pos);
        }
    }
    hero.move(redX);
}

// Summon soldiers, then command them to move
while (hero.gold > hero.costOf("soldier")) {
    hero.summon("soldier");
}
let army = hero.findFriends();
for (let soldier of army) {
    hero.command(soldier, "move", {x:55, y:46});
}
hero.moveXY(55, 46);

// Command soldiers and hero to attack
while (true) {
    let enemies = hero.findEnemies();
    let soldiers = hero.findFriends();
    for (let i=0; i<soldiers.length;i++) {
        hero.command(soldiers[i], "attack", enemies[i]);
    }
    let enemy = hero.findNearestEnemy();
    if (hero.isReady("chain-lightning")) {
        hero.cast("chain-lightning", enemy);
        hero.say("CASTING");
    } else {
        hero.attack(enemy);
    }
}