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

[SOLVED] Gas Attack Help (JS) - Sarven Desert

I’ve been stuck on this level for a long time. Please tell me what is wrong with my code!

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) {
        
    
        // Add the current enemy's health to totalHealth
    enemies.health + totalHealth;
        // Increment enemyIndex by 1.
        enemyIndex += 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.");

My character just tells the cannon to fire 0 grams of poison.

You have to add a single enemy’s health to total health rather than the array enemies, which doesn’t have a .health property.
Danny

I tried doing that but it still does the same thing.

Please could you post your new code.
Thanks
Danny

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) {
        
    
        // Add the current enemy's health to totalHealth
    enemy.health + totalHealth;
        // Increment enemyIndex by 1.
        enemyIndex += 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.");

I think I’ve found the problem: you’re not updating totalHealth for each enemy, you’re just saying

That’s not defining anything. Remember to use = and start with totalHealth.
e.g.

totalHealth = # total health plus the health of the enemy

Also your return total health line is inside the while loops curly bracket:

You don’t want it to run every time the while loop loops, so you need to put it outside the curly bracket.
Danny

I tried that now. My character tells the cannon to fire NaN grams of poison. The cannon didn’t fire anything.

Here is the new code:

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) {
        
    
        // Add the current enemy's health to totalHealth
    totalHealth = enemies.health + totalHealth;
        // Increment enemyIndex by 1.
        enemyIndex += 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.");

Getting closer…but, a question tho:

What does enemies.health equal?

As Danny mentioned, the variable ‘enemies’ represents an array, a collection of objects, so does not have a .health property of its own. You need to define a new variable, like ‘enemy’, using enemies as the source. This new variable will represent a single object (element) of the array and thereby, does have a .health property.

As you iterate through the enemies array, add the health of each one of these individual elements to the counter, totalHealth.

I tried changing enemies.health to enemy.health and adding var enemy but it says: Cannot read property ‘health’ of null. Is it something to do with where I kept the var enemy?

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) {
        
    var enemy = hero.findNearestEnemy();
    
        // Add the current enemy's health to totalHealth
    totalHealth = enemy.health + totalHealth;
        // Increment enemyIndex by 1.
        enemyIndex += 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.");

With your new code, your are iterating through the list of enemies, but are only adding the nearest’s health to totalHealth…

Remember how you identify individual elements of an array? These elements are represented as numeric positions within the array…the first element would be [0], the next [1], and so on.

You are already iterating (looping) through the enemies array, with your statement 'while (enemyIndex < enemies.length). ‘enemyIndex’ is the counter, which also equates to the position of the current element being examined. From this, you can get the individual health values you need…enemies[0].health, then enemies[1].health, and so on. Only, you use your counter to specify the element position, not the actual numbers.

Does that mean I have to use enemyIndex instead of enemy.health?

I tried using enemyIndex and my character tells the cannon to fire 45 grams, which isn’t enough to kill all the enemies. The cannon only killed 1 munchkin.

Kind of, yes; enemyIndex needs to be incorporated into your solution. Remember, it is the counter being used to indicate which array element is currently being examined. At the start of the loop, it is equal to zero…as the loop iterates, it is incremented by 1, repeating this until all elements in the array have been examined.

So, you end up with:

enemyIndex = 0
totalHealth = 0

  1. (first loop) while enemyIndex < enemies.length
    a. totalHealth += enemies[enemyIndex].health (add the health of the currently focused element)
    b. enemyIndex += 1 (increment the counter, starting the next loop)
  2. return totalHealth after the final loop

FINALLYY! I did it! Thanks for all your help, Danny and dedreous! :smiley:

2 Likes

Even though all of you gave tips to complete this level, I still can’t do it. Whats wrong with my code?

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) {
        
    var enemy = hero.findNearestEnemy();
    
        // Add the current enemy's health to totalHealth
    totalHealth += enemy.health;
        // Increment enemyIndex by 1.
        enemyIndex += 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();

I see two issues so far. The first is in your function:

Since you are already passing the ‘enemies’ array to the function (via function sumHealth(enemies)), this line is not needed…delete it. Besides, the way it’s written, it is only counting the closest enemy, over and over and over…

Two: You are not calling your function…did you accidentally not copy all of your code? The last couple of lines are missing.

1 Like

I see the problems that you told me to do and it seems that I am in the same problem as Kementari with the cannon only firing 45 grams. Yet I do not understand the tip that you helped Kementari to solve this problem. Could you rephrase it maybe? Here’s my code if it helps. (All of it this time :slight_smile:)

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) {
        // Add the current enemy's health to totalHealth
        totalHealth += enemyIndex;
        // Increment enemyIndex by 1.
        enemyIndex += 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.");

Which enemy are you targeting here?:

    while (enemyIndex < enemies.length) {
        // Add the current enemy's health to totalHealth
        totalHealth += enemyIndex;
        // Increment enemyIndex by 1.
        enemyIndex += 1;
1 Like