I don't know why my code for uneasy truce doesn't work. - Javascript


#1

// 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.
if (findSouthernUnits(enemies) > friends) {
hero.summon(“soldier”);
}
// If there are more ogres south of you than friends.
// Then summon another “soldier”.
}

Shouldn’t it work?


#2
if (findSouthernUnits(enemies) > friends) {
hero.summon("soldier");
}

Compare arrays length
length is represent amount of units

findSouthernUnits(enemies).length
friends.length


#3

Thanks @htrnblr that helps so much. I can’t believe I didn’t realise “Facepalm”