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.");
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?
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.”)
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
while index < len(enemies):
totalHealth = enemies[index].health # you should add to totalHealth
index += enemies.health # this index should be incremented by 1