I’m still having trouble with this level. Can you guys please tell me what I’m doing wrong?
// You have one arrow. Make it count!
// This should return the enemy with the most health.
function findStrongestEnemy(enemies) {
var strongest = null;
var strongestHealth = 0;
var enemyIndex = 0;
while (enemyIndex < enemies.length) {
var enemy = enemies[0];
if (enemy.health > strongestHealth) {
enemy.health = strongestHealth;
}
enemyIndex = enemyIndex + 1;
return strongest;
}
}
var enemies = hero.findEnemies();
var leader = findStrongestEnemy(enemies);
if (leader) {
hero.say(leader);
}
One simple detail, the variable being assigned the enemy.health needs to be on the left side of the = sign.
Also, when you use the while loop with and index, you need to use the index for the array index so it goes through each enemy in the enemies list.
while (enemyIndex < enemies.length) {
var enemy = enemies[0]; // don't want use hard coded number index, use the enemyIndex variable
To make the function work properly, you also have to assign the enemy that has the most health to the strongest variable. I suggest you add that line right after assigning the strongestHealth variable.
It says that because I had another topic but it closed, so I had to create a new one. The person I was talking about was the person that tried to help me in the previous topic. As for the feedback, here is my code after applying all the feedback I got here:
// You have one arrow. Make it count!
// This should return the enemy with the most health.
function findStrongestEnemy(enemies) {
var strongest = null;
var strongestHealth = 0;
var enemyIndex = 0;
while (enemyIndex < enemies.length) {
var enemy = enemies[0];
if (enemy.health > strongestHealth) {
strongestHealth = enemy.health;
}
enemyIndex = enemyIndex + 1;
return strongest;
}
}
var enemies = hero.findEnemies();
var leader = findStrongestEnemy(enemies);
if (leader) {
hero.say(leader);
}
I did everything you guys said, but it still doesn’t work.
My character doesn’t do anything!
Look again at what @brooksy125 says. He gives you 3 things to change - you’ve got the first one right, but the other two still need sorting (you had the third thing in your code before, so look back at that if you want).
Also swap these 2 lines:
Try making changes, post your new code if you want more help, and we’ll keep trying to give hints!
Here is my code after applying that feedback. It still says that it “Cannot read property ‘health’ of undefined” though. What am I doing wrong now?
// You have one arrow. Make it count!
// This should return the enemy with the most health.
function findStrongestEnemy(enemies) {
var strongest = null;
var strongestHealth = 0;
var enemyIndex = 0;
while (enemyIndex < enemies.length) {
var enemy = enemyIndex[0];
if (enemy.health > strongestHealth) {
enemy.health = strongestHealth;
}
enemyIndex++;
return strongest;
}
}
var enemies = hero.findEnemies();
var leader = findStrongestEnemy(enemies);
if (leader) {
hero.say(leader);
}
I did that, but now it still says that. Is there anything else I’m doing wrong? Here is my code:
// You have one arrow. Make it count!
// This should return the enemy with the most health.
function findStrongestEnemy(enemies) {
var strongest = null;
var strongestHealth = 0;
var enemyIndex = 0;
while (enemyIndex < enemies.length) {
var enemy = enemyIndex[0];
if (strongestHealth > enemy.health) {
enemy.health = strongestHealth;
}
enemyIndex =+ 1;
return strongest;
}
}
var enemies = hero.findEnemies();
var leader = findStrongestEnemy(enemies);
if (leader) {
hero.say(leader);
}
I did 2 and 3. I just don’t know what to update the “strongest” variable to. Also, I don’t know what you mean in 1. I used flags to navigate the path, so that doesn’t help much. Can you please explain that to me? Here is my code:
// You have one arrow. Make it count!
// This should return the enemy with the most health.
function findStrongestEnemy(enemies) {
var strongest = null;
var strongestHealth = 0;
var enemyIndex = 0;
while (enemyIndex < enemies.length) {
var enemy = enemyIndex[0];
if (strongestHealth > enemy.health) {
strongestHealth = enemy.health;
}
enemyIndex =+ 1;
}
return strongest;
}
var enemies = hero.findEnemies();
var leader = findStrongestEnemy(enemies);
if (leader) {
hero.say(leader);
}
Ok, I did that, but my code still doesn’t work! Is there anything else that I’m doing wrong here? Here is my code:
// You have one arrow. Make it count!
// This should return the enemy with the most health.
function findStrongestEnemy(enemies) {
var strongest = null;
var strongestHealth = 0;
var enemyIndex = 0;
while (enemyIndex < enemies.length) {
var enemy = enemies[enemyIndex]
if (strongestHealth > enemy.health) {
strongestHealth = enemy.health;
strongest = enemy;
}
enemyIndex =+ 1;
}
return strongest;
}
var enemies = hero.findEnemies();
var leader = findStrongestEnemy(enemies);
if (leader) {
hero.say(leader);
}
Ok, I did that, but my code still doesn’t work. Here is my code:
// You have one arrow. Make it count!
// This should return the enemy with the most health.
function findStrongestEnemy(enemies) {
var strongest = null;
var strongestHealth = 0;
var enemyIndex = 0;
while (enemyIndex < enemies.length) {
var enemy = enemies[enemyIndex];
if (enemy.health > strongestHealth) {
strongestHealth = enemy.health;
strongest = enemy;
}
enemyIndex =+ 1;
}
return strongest;
}
var enemies = hero.findEnemies();
var leader = findStrongestEnemy(enemies);
if (leader) {
hero.say(leader);
}
I reversed it, but it still doesn’t work. Here is my code after:
// You have one arrow. Make it count!
// This should return the enemy with the most health.
function findStrongestEnemy(enemies) {
var strongest = null;
var strongestHealth = 0;
var enemyIndex = 0;
while (enemyIndex < enemies.length) {
var enemy = enemies[enemyIndex];
if (enemy.health > strongestHealth) {
strongest = enemy;
strongestHealth = enemy.health;
}
enemyIndex =+ 1;
}
return strongest;
}
var enemies = hero.findEnemies();
var leader = findStrongestEnemy(enemies);
if (leader) {
hero.say(leader);
}