Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Brittle Morale [JavaScript]

Does anyone know what is wrong with the following code?

My character just stands there without an error sign

menacingly

// You have one arrow. Make it count!
// This should return the enemy with the most health.
function findStrongestEnemy(enemies) {
    var strongest = null;
    var strongestHealth = 0;
    var enemyIndex = 0;
    // While enemyIndex is less than the length of enemies:
    while (enemies > enemyIndex) {
        // Set an enemy variable to enemies[enemyIndex]
        var enemy = enemies[enemyIndex];
        // If enemy.health is greater than strongestHealth
        if (enemy.health > strongestHealth) {
            // Set `strongest` to enemy
            // Set strongestHealth to enemy.health
            strongest = enemy;
            strongestHealth = enemy.health;
        }
        // Increment enemyIndex
        enemyIndex ++;
    }
    return strongest;
}

var enemies = hero.findEnemies();
var leader = findStrongestEnemy(enemies);
if (leader) {
    hero.say(leader);
}

Have a closer look at the comment:

    // While enemyIndex is less than the length of enemies:

I made the change but no dice. Any other things you might see that could be causing this?

There were actually two issues with your while statement…less than and length

Actually, I just tested with your code and the order you have the statements will work with >. Post your while statement again please.

1 Like

Matters no more, as the length was the problem. Thanks!

1 Like