Stuck at weakest quickest (Sarwen) jsc

Hello

Have some issue at this Sarwen level.
When I launch, it every times start by the “strongest” one instead of the weakest and I dont know why.

Thanks for you help, here is my code :

// Defeat shamans to survive.

// This function finds the weakest enemy.
function findWeakestEnemy() {
    var enemies = hero.findEnemies();
    var weakest = null;
    var leastHealth = 99999;
    var enemyIndex = 0;
    // Loop through enemies:
    while(enemyIndex < enemies.length) {
        // If an enemy's health is less than leastHealth:
        var enemy = enemies[enemyIndex];
        if (enemy.health < leastHealth) {
            // Make it the weakest 
            weakest = enemy;
            // and set leastHealth to its health.
            leastHealth = enemy.health;
            enemyIndex +=1;
        }
    
    return weakest;
    }

}

while(true) {
    // Find the weakest enemy with the function:
    var weakest = findWeakestEnemy();
    // If the weakest enemy exists:
    if (weakest) {
        // Attack it!
        hero.attack(weakest);
    }
}

Find help somewhere else.

Mistake about return inside the loop and index increment in If statement.

Sorry.

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