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