Need help with Gas Attack JS


#1

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

#2

countHit == countHit + units.health;

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


#3

something like this?

countHit == countHit + units[i].health


#4

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


#5

Thank you!:slight_smile:


#6

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?


#7

What are doing here?


#8

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


#9

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


#10

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.
    

return totalHealth

‘# 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.”)


#11

it says unknown unary operater Uadd


#12

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


#13

don’t know what you mean


#14

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


#15

just said unknown unary operator: UAdd


#16

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
    

return totalHealth

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


#17

Please format your code according to the FAQ


#18

@lazerA.Y Your problem lies here:

while index < len(enemies):
    totalHealth = enemies[index].health   # you should add to totalHealth
    index += enemies.health   # this index should be incremented by 1