Borrowed Sword Please Help

Please help I I’ve been stuck for a while

function findStrongestEnemy() {
    var enemies = hero.findEnemies();
    var highestHealth = null;
    var health = 0;
    for (var i = 0; i < enemies.length; i++) {
        var enemy = enemies[i];
        if (enemy.health >= health) {
            highestHealth = enemy;
            health = enemy.health;
            return highestHealth;
        }
    }
}
while (true) {
    var enemies = hero.findEnemies();
    var friends = hero.findFriends();
    var highestHealth = null;
    var health = 0;
    for (var i = 0; i < enemies.length; i++) {
        var enemy = enemies[i];
    }
    if (enemy.health >= health) {
        highestHealth = enemy;
        health = enemy.health;
        for (var i = 0; i < friends.length; i++) {
            var friend = friends[i];
            if (highestHealth) {
                hero.command(friend, "attack", highestHealth);
            }
        }
    }
    findStrongestEnemy;
    for (var i = 0; i < friends.length; i++) {
        findStrongestEnemy;
    }
}
1 Like

Please give more information about your issue such as a screenshot and any errors that pop up when you run your code

@Luke10
:fox_face::fox_face::fox_face:

There are no errors that pop up. The objective ends as incomplete because all of the archers die.

1 Like

I made a little amount of progress as now only one Yeti lives but the archers die so it’s not complete. I updated the code to my more effective one

1 Like

Why are you using two find strongest enemies? the one in the while true loop is enough.
:lion: :lion: :lion:

Hello guys, this is a friendly reminder that this topic is for assisting @monsterjam23 with code, not talking about why some members put an emoji at the end of their sentences. @SuperSmacker, please bring these kind of discussions in PMs. Thank you.

1 Like

eventually I did it, put find maxHealth inside the friend range

[Mod edit: Please do not post solutions]

I solved using 1 while loop and including 2 for loops inside the while statement and 1 if statement. The first for loop iterates through the enemies list to find “if” target is the toughest and the second for loop iterates through the friends list and then hero.command friends to attack toughest.

The process works because it is all inside the while statement.

  • executes the first for loop.
  • executes the second for loop.
  • repeats as long as true;

Below is one format with some information on how you could structure a solution but you fill in the blanks.

while (true) {

var friends = hero.findFriends();
var enemies = hero.findEnemies();
var highestHealth = 0;
var toughest = null;

for (var i = 0; i < ???; i += 1) {
var enemy = ???; // Remember how you would iterate through each enemy from an array

if (??? > highestHealth) { // Think about what enemy we are comparing to find highest health
    toughest = enemy;
    highestHealth = enemy.health;
      }
   }

for (var i = 0; i < friends.length; i += 1) {
var friend = friends[i];
hero.command(friend, “attack”, ???); // Think about which enemy we found
   }
}

P.S. if you are more comfortable using i++ instead of i += 1 they both work effectively, however take note that the i++ will only increase/increment by 1 while i += (any positive number) has more flexibility.

The principle is the same for the (-) as well just swap the plus signs for decreasing.

At the bottom of your page you have the things that look like brackets. I THINK that you should delete those and you could delete"var health = 0;
for (var i = 0; i < enemies.length; i++) {