Need help with Gas Attack JS

Hello. Need help with this lvl. How I shoud count enemy health? What is wrong?

``````// Peasants are trapped in a valley!
// You only have one poison shell.
// Calculate the required mass of the shell to defeat the ogres.
// The poison mass needs to be equal to the health of the ogres.
// Using too little or too much will be a disaster!

// This function should return the total health of the enemies:
function healthSum(units) {
// Create a variable and set it to 0 to start the sum.
var countHit =0;
var i = 0;
// Iterate over each enemy in the array and add their health to the sum variable.
while(i< units.length){

countHit == countHit + units.health;
i++;
}

return countHit; // ∆ Change this to return the sum variable!
}

// 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.
hero.say("Use " + healthSum(enemies) + " grams.");
``````

`countHit == countHit + units.health;`

`units` is an array of units. First you need to get the certain unit `units[i]` and read its health.

something like this?

countHit == countHit + units[i].health

Yep. But `==` is a comparison operator. Use `=` to assign values.

Thank you!

def healthSum(units):
# Create a variable and set it to 0 to start the sum.
ogres=hero.findEnemies()
ogreHealth=0
ogreIndex=0
ogre=ogres[ogreIndex]
while ogreIndex<len(ogres):
ogreHealth+=1
ogreIndex+=1
return ogreHealth

Use the cannon to defeat the ogres.

cannon = hero.findNearest(hero.findFriends())

The cannon can see through the walls.

enemies = cannon.findEnemies()

Calculate the sum of the ogres’ health.

hero.say(“Use " + healthSum(enemies) + " grams.”)

My code is not working properly and I do not know why. Can anyone help me?

What are doing here?

I think at some point I modified the code without noticing. It is supposed to be ogreHealth+=ogre.health. Even then. It does not work

Could you post your new code? And please post it as code (use triple ` to wrap it)

need help
’# Calculate the total health of all the ogres.

def sumHealth(enemies):
# Create a variable and set it to 0 to start the sum.
totalHealth = 0
# Initialize the loop index to 0
loop:
index = 0
while index < len(enemies):
totalHealth = enemies[index].value
index =+ 1
# While index is less than the length of enemies array

``````    # Add the current enemy's health to totalHealth

# Increment the index.

``````

‘# Use the cannon to defeat the ogres.
cannon = hero.findNearest(hero.findFriends())
’# The cannon can see through the walls.
enemies = cannon.findEnemies()
’# Calculate the sum of the ogres’ health.
ogreSummaryHealth = sumHealth(enemies)
hero.say(“Use " + ogreSummaryHealth + " grams.”)

it says unknown unary operater Uadd

I dont think value is the method you want. If you want the health use `.health`

1 Like

don’t know what you mean

If you want to know the health of an unit you should read `.health` property. `.value` is a coin/gem property.

just said unknown unary operator: UAdd

here’s my code:

Calculate the total health of all the ogres.

def sumHealth(enemies):
# Create a variable and set it to 0 to start the sum.
totalHealth = 0
# Initializhe loop index to 0
index = 0
while index < len(enemies):
totalHealth = enemies[index].health
index += enemies.health
# While index iess than the length of enemies arra
# Add the current enemy’s health to totalHealth

``````    # Increment the ind

``````

Use the cannon to defeat the ogres.

cannon = hero.findNearest(hero.findFriends())

The cannon can see through the walls.

enemies = cannon.findEnemies()

Calculate the sum of the ogres’ health.

ogreSummaryHealth = sumHealth(enemies)
hero.say(“Use " + ogreSummaryHealth + " grams.”)

I think it’s fine, but it still dosen’t work because all the ogres don’t die and they kill the peasents

``````while index < len(enemies):