Mix Unit Tactics got over, but got only one archer

This is my code. I passed the level, but I’m not happy because I can’t get more than one archer.
If I change the code, I get more archers but no gold.
Help please

    // Practice using modulo to loop over an array

// Choose the mix and order of units you want to summon by populating this array:
var summonTypes = ["archer","archer","archer","soldier","archer","soldier","archer","soldier"];
var defendPoints = [{"x": 40, "y": 42},{"x": 40, "y": 26},{"x": 40, "y": 38},{"x": 40, "y": 33}];

this.getCoins = function() {
    loop {
        var coins = this.findItems();
        var coinIndex = 0;
        var coin = coins[coinIndex];
        if (coin) {
            this.move(coin.pos);
        }
    }
};

this.summonTroops = function() {
    var type = summonTypes[this.built.length % summonTypes.length];
    if(this.gold >= this.costOf(type)) {
        this.summon(type);
    }
};

this.commandTroops = function() {
    var friends = this.findFriends();
    for(var friendIndex=0; friendIndex < friends.length; friendIndex++) {
        var friend = friends[friendIndex];
        var point = defendPoints[friendIndex % defendPoints.length];
        if (friend.type == "archer"){
            this.command(friend, "defend", point);
        }
    }
};

loop{
        this.summonTroops();
        this.commandTroops();
        this.getCoins();
}

Thanks

// Practice using modulo to loop over an array

// Choose the mix and order of units you want to summon by populating this array:
var summonTypes = [“archer”,“archer”,“archer”,“soldier”,“archer”,“soldier”,“archer”,“soldier”];
var defendPoints = [{“x”: 40, “y”: 42},{“x”: 40, “y”: 26},{“x”: 40, “y”: 38},{“x”: 40, “y”: 33}];

this.getCoins = function() {
loop {
var coins = this.findItems();
var coinIndex = 0;
var coin = coins[coinIndex];
if (coin) {
this.move(coin.pos);
}
}
};

this.summonTroops = function() {
var type = summonTypes[this.built.length % summonTypes.length];
if(this.gold >= this.costOf(type)) {
this.summon(type);
}
};

this.commandTroops = function() {
var friends = this.findFriends();
for(var friendIndex=0; friendIndex < friends.length; friendIndex++) {
var friend = friends[friendIndex];
var point = defendPoints[friendIndex % defendPoints.length];
if (friend.type == “archer”){
this.command(friend, “defend”, point);
}
}
};

loop{
this.summonTroops();
this.commandTroops();
this.getCoins();
}

Please format your code according to the FAQ

Hmm. Could you give me more information as to your problem? It seems like you should get a mix of archers and soldiers!

Hello, Ciencia, and welcome. Please read the FAQ before you post again, so that you learn how to format your code properly. I’ve done it for you this time, but do so yourself in the future.

Your problem is that you have a loop inside the function this.getCoins. This means that once the hero enters the function, it ill never exit out, and continue to collect coins. Remove the loop, and it should be fine.

1 Like

Thanks a lot. It worked quite well. Sorry about the indentation.