The Two Flowers JS problem

Hello everyone!
I had new questions about JS =)
Now I have problem with level “The Two Flovers”
My code doesn’t work. Here it is:

function summonSoldiers() {
    if (hero.gold >= hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

function commandSoldiers() {
    var friends = hero.findByType("soldier");
    for(var i = 0; i < friends.length; i++){
        var friend = friends[i];
        var enemy = hero.findNearestEnemy();
        if(enemy && friend.distanceTo(enemy) < 5) {
            hero.command(friend, "attack", enemy);
        } else {
            hero.command(friend, "move", peasant.pos);
        }
    }
}

function pickUpNearestCoin(){
    hero.move((hero.findNearest(hero.findItems())).pos);
}

var peasant = hero.findByType("peasant")[0];

while(true) {
    summonSoldiers();
    commandSoldiers();
    pickUpNearestCoin();
}

In the commandSoldiers function at first you wrote
var enemy = hero.findNearestEnemy();
and then (in the code with which you completed the level) you wrote
var enemy = friend.findNearestEnemy();
It is really important to remember that it’s better and easier for soldiers to attack their nearest enemy, not yours.
If I made it clear, could you please remove the working code from the topic because sharing solutions is not allowed in this forum? Thanks.

1 Like

Thanks you!
I did what do u ask. Now here is only my first code

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.