[Solved]Sarven Desert -GAS Attack

i don’t understand how to Calculate the total health of all the ogres.
what i’m doing wrong?

// Calculate the total health of all the ogres.

function sumHealth(enemies) {
    // Create a variable and set it to 0 to start the sum.
    var totalHealth = 0;
    // Initialize the loop index to 0
    var enemyIndex = 0;
    // While enemyIndex is less than the length of enemies array
    while(enemyIndex < enemies.length) {
        let enemy = enemies[enemyIndex];
        while(enemy.health > totalHealth) {
        // Add the current enemy's health to totalHealth
        totalHealth = enemy.health;
        }
        enemyIndex++;
    }
        // Increment enemyIndex by 1.
        
    return totalHealth;
}

// Use the cannon to defeat the ogres.
var cannon = hero.findNearest(hero.findFriends());
// The cannon can see through the walls.
var enemies = cannon.findEnemies();
// Calculate the sum of the ogres' health.
var ogreSummaryHealth = sumHealth(enemies); 
hero.say("Use " + ogreSummaryHealth + " grams.");

some one help me please.

find the sum of all of the enemies by doing the math yourself I came up with 42.

how to do this it’s all time random health

no It’s not all random just keep submitting until it works

i passed this level. but it’s doesnn’t help me how it work((

You would add the enemy’s health to the totalHealth. So it would be totalHealth = totalHealth + enemy.health.

What @abc says. Plus you need to delete this line:

       while(enemy.health > totalHealth) {

Jenny

if i access directly to array it’s work. Thanks

thanks Jenny for help

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