Help mountain mercenary javascript

// Gather coins to summon soldiers and have them attack the enemy.

while(true) {
    // Move to the nearest coin.
    // Use move instead of moveXY so you can command constantly.
    var item = hero.findNearestItem();
    hero.move(item.pos);
    
    // If you have funds for a soldier, summon one.
    if (hero.gold > hero.costOf("soldier")) {
        hero.summon("soldier");
    }
    var enemy = hero.findNearest(hero.findEnemies());
    if (enemy) {**strong text**
        
        var soldiers = hero.findFriends();
        // Loop over all your soldiers and order them to attack.
        for (soldier in soldiers) {
            // Use the 'attack' command to make your soldiers attack.
            hero.command(soldier, "attack", enemy);
            soldierIndex += 1;
        }
    }
}

@Falcons118 or @Deadpool198 or @milton.jinich

try putting a if after the loop since you may not have any alive troops.

No, the soldiers never attack. Ever.

Try putting this in a if. Than your code is identical to mine(but in JS)

I think it’s the for loop.

well, soldierIndex isn’t defined.

put soldierIndex = 0 out of the loop.

@Dragonlouis
instead of

 for (soldier in soldiers) 

change that too

for (var i=0; i<soldiers.length;i++){
var friend=soldiers[i];
var enemy= friend.findNearestEnemy();
//now if friend.type=="soldier" and there is an enemy
command friend to attack the enemy
}

also put soldierIndex = 0; in there

well you dont really need the soldierIndex

for var soldier in soldiers

no do what I said in the post above

this should work

because I dont think you are looping over all of your soldiers

if you want to do soldierIndex += 1 you have to add it.