Level: Borrowed Sword Help

Hello,
I’ve been stuck on this level for a while now, and it seems that the archers are attacking the ogres first instead of the enemy with the highest health. Everything else works fine, though.
My code:

function findStrongestEnemy(enemies){
    var enemyIndex = 0;
    var strongest = null;
    var strongestHealth = 0;
    while (enemyIndex < enemies.length){
        var enemy = enemies[enemyIndex];
        if (enemy.health > strongestHealth){
            strongest = enemy;
            strongestHealth = enemy.health;
        }
        enemyIndex++;
    }
    return strongest;
}
var enemies = hero.findEnemies();
var target = findStrongestEnemy(enemies);
var freinds = hero.findFriends();
var freindIndex = 0;
var attackers = freinds[freindIndex];
if (target){
    freindIndex++;
    hero.command(attackers, "attack", target);
}

The problem is that two yetis make it out in the end.

Welcome to the discourse, @CodeNinja-Elija! I haven’t learned JavaScript(I think that’s what that language is, sorry if I’m wrong :sweat_smile:), so I can’t help, sorry. :frowning:

:email: Rachel :email:

1 Like

Can you post a link to the level please?

Here you go(it is a subscriber level only)
CodeCombat - Coding games to learn Python and JavaScript?

1 Like

Hi CodeNinja-Elija,

Two things:

  • Stick all the code except the function (ie everything from var enemies = hero.findEnemies() onwards) into a while(true) loop

  • You want a bit more code so that the hero commands each of the friends to attack - at the moment only one of them will get the command.

    Jenny

1 Like

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