[SOLVED] Uneasy Truce Yetis

I don’t understand why the yeti’s keep on attacking me.

// Summon one soldier for every ogre to the south of you!
// Don't count the ogres to the north!

// Accept an array of units as the parameter.
// Return only the units to the south of the hero.
function findSouthernUnits(units) {
    var southernUnits = [];
    for(var i=0; i < units.length; i++) {
        var unit = units[i];
        if(unit.pos.y < hero.pos.y) {
            // Add the unit to the array with: push()
            southernUnits.push(unit);
        }
    }
    return southernUnits;
}
while(true) {
    var friends = hero.findFriends();
    var enemies = hero.findEnemies();
    // Use findSouthernUnits to get enemies to the south.
    findSouthernUnits(enemies);
    // If there are more ogres south of you than friends.
    if (findSouthernUnits(enemies).length > findSouthernUnits(friends).length) {
        // Then summon another "soldier".
        hero.summon("soldier");
        
    }
}

1 Like

Just put friends.length in rather than putting friends through the function.

1 Like
// Summon one soldier for every ogre to the south of you!
// Don't count the ogres to the north!
// Accept an array of units as the parameter.
// Return only the units to the south of the hero.
function findSouthernUnits(units) {
    var southernUnits = [];
    for (var i = 0; i < units.length; i++) {
        var unit = units[i];
        if (unit.pos.y < hero.pos.y) {
            // Add the unit to the array with: push()
            southernUnits.push(unit);
        }
    }
    return southernUnits;
}
while (true) {
    var friends = hero.findFriends();
    var enemies = hero.findEnemies();
    // Use findSouthernUnits to get enemies to the south.
    findSouthernUnits(enemies);
    // If there are more ogres south of you than friends.
    if (enemies.length > friends.length) {
        // Then summon another "soldier".
        hero.summon("soldier");
    }
}
It summons them just fine until one of the ogres at the bottom says attack. Then they start fighting and I don't know how to stop that.
1 Like

Welcome back, @ToxicParasite! :partying_face:

On this line put

enemies = findSouthernUnits(enemies);

Does it work now?

1 Like

Oh wow thanks guys it works. I just don’t know why that makes a difference.

1 Like

You remain out of gold if you summon for all the enemies, there with my text you made enemies the enemies that you have to summon troops for. Do you understand now?

2 Likes

Oh I get it now thank you. I haven’t done this in a while and I forgot that you need gold to summon troops.

1 Like

No problem! Glad I could help! How mark the solution.

1 Like

Do you know how to mark the solution?

1 Like

What do you mean by that?

Oh, you marked the solution! Then congratulations for completing the level! :partying_face:

1 Like