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

**URL:** <https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434>\
**Category:** Level Help\
**Created:** [May 6, 2020, 3:24pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434 "2020-05-06T15:24:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kementari](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kementari/32/12678_2.png) [@Kementari](https://discourse.codecombat.com/u/Kementari)\
**Post date:** [May 6, 2020, 3:24pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/1 "2020-05-06T15:24:52Z")

</div>

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

```python
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.");

```

---

<div class="post-metadata">

**Author:** ![Kementari](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kementari/32/12678_2.png) [@Kementari](https://discourse.codecombat.com/u/Kementari)\
**Post date:** [May 6, 2020, 3:26pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/2 "2020-05-06T15:26:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [May 6, 2020, 4:51pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/3 "2020-05-06T16:51:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kementari](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kementari/32/12678_2.png) [@Kementari](https://discourse.codecombat.com/u/Kementari)\
**Post date:** [May 7, 2020, 5:25am UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/4 "2020-05-07T05:25:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [May 7, 2020, 9:35am UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/5 "2020-05-07T09:35:59Z")

</div>

Please could you post your new code.  
Thanks  
Danny

---

<div class="post-metadata">

**Author:** ![Kementari](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kementari/32/12678_2.png) [@Kementari](https://discourse.codecombat.com/u/Kementari)\
**Post date:** [May 7, 2020, 10:09am UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/6 "2020-05-07T10:09:13Z")

</div>

```javascript
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.");

```

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [May 7, 2020, 10:39am UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/9 "2020-05-07T10:39:59Z")

</div>

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

> [@Kementari](#):
>
> ```python
> enemy.health + totalHealth;
> 
> ```

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

```python
totalHealth = # total health plus the health of the enemy

```

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

> [@Kementari](#):
>
> ```python
> return totalHealth; 
> }
> }
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![Kementari](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kementari/32/12678_2.png) [@Kementari](https://discourse.codecombat.com/u/Kementari)\
**Post date:** [May 8, 2020, 2:48pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/10 "2020-05-08T14:48:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kementari](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kementari/32/12678_2.png) [@Kementari](https://discourse.codecombat.com/u/Kementari)\
**Post date:** [May 8, 2020, 2:51pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/11 "2020-05-08T14:51:29Z")

</div>

Here is the new code:

```python
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.");

```

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [May 8, 2020, 3:06pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/12 "2020-05-08T15:06:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kementari](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kementari/32/12678_2.png) [@Kementari](https://discourse.codecombat.com/u/Kementari)\
**Post date:** [May 8, 2020, 3:46pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/13 "2020-05-08T15:46:45Z")

</div>

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?

```python
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.");

```

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [May 8, 2020, 3:58pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/14 "2020-05-08T15:58:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kementari](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kementari/32/12678_2.png) [@Kementari](https://discourse.codecombat.com/u/Kementari)\
**Post date:** [May 9, 2020, 2:16pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/15 "2020-05-09T14:16:17Z")

</div>

> [@Kementari](#):
>
> totalHealth = enemy.health + totalHealth;

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

---

<div class="post-metadata">

**Author:** ![Kementari](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kementari/32/12678_2.png) [@Kementari](https://discourse.codecombat.com/u/Kementari)\
**Post date:** [May 9, 2020, 2:20pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/16 "2020-05-09T14:20:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [May 9, 2020, 2:52pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/17 "2020-05-09T14:52:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kementari](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kementari/32/12678_2.png) [@Kementari](https://discourse.codecombat.com/u/Kementari)\
**Post date:** [May 9, 2020, 2:55pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/18 "2020-05-09T14:55:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mitboy3](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/mitboy3/32/13246_2.png) [@mitboy3](https://discourse.codecombat.com/u/mitboy3)\
**Post date:** [August 11, 2020, 2:36am UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/19 "2020-08-11T02:36:55Z")

</div>

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

```javascript
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();

```

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [August 11, 2020, 1:32pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/20 "2020-08-11T13:32:54Z")

</div>

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

> [@mitboy3](#):
>
> `var enemy = hero.findNearestEnemy();`

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.

---

<div class="post-metadata">

**Author:** ![mitboy3](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/mitboy3/32/13246_2.png) [@mitboy3](https://discourse.codecombat.com/u/mitboy3)\
**Post date:** [August 11, 2020, 1:57pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/21 "2020-08-11T13:57:41Z")

</div>

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 🙂)

```javascript
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.");

```

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [August 11, 2020, 2:34pm UTC](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434/22 "2020-08-11T14:34:43Z")

</div>

Which enemy are you targeting here?:

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

```

[Next page](https://discourse.codecombat.com/t/solved-gas-attack-help-js-sarven-desert/21434.md?page=2)
