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

1 Like

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

1 Like

No, the soldiers never attack. Ever.

1 Like

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

1 Like

I think it’s the for loop.

1 Like

well, soldierIndex isn’t defined.

1 Like

put soldierIndex = 0 out of the loop.

1 Like

@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
}
1 Like

also put soldierIndex = 0; in there

1 Like

1 Like

well you dont really need the soldierIndex

1 Like

for var soldier in soldiers

1 Like

no do what I said in the post above

this should work

1 Like

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

1 Like

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

1 Like