``````// 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 is less than the length of enemies:
while(enemyIndex < enemies.length) {
var enemy = enemies[enemyIndex];
if(enemy.health > strongestHealth) {
strongestHealth = enemy.health;
strongest = enemy;
}
enemyIndex ++;
}
return strongest;
}

var enemies = hero.findEnemies();
}
``````

Hey guys. Can someone please explain how does this area work? Thanks!
if(enemy.health > strongestHealth) {
strongestHealth = enemy.health; <---- why do we do that equality
strongest = enemy;
}

If you donâ€™t set the strongestHealth to the enemyâ€™s health then the strongestHealth will stay the same and all the enemies will be strongest and you would have the wrong enemy.

1 Like

Yes. I see.
But I cant get the logic. How exactly this hapens.

I think it explains it in the hints and in other levelâ€™s hints which use arrays, I know ho they work but I think the hints will better explain it.

1 Like

enemyIndex = 0
strongesthealth = 0

while(enemyIndex < enemies.length)

while(0 is below < number of enemies)
enemy = enemies[enemyIndex] (0)

lets say first monster hp is 10
if 10 is > 0;
strongesthealth become 10.
strongest = enemy (0)

enemyIndex ++
enemyindex iterate from 0 to 1.

while 1 < enemy.length:
enemy = enemies[enemyindex] (1)

lets say enemy1 hp is 20 now.

if 20 (enemy.health) > strongestHealth (10)
yes 20 is > 10.
so strongesthealth become 20
and strongest become enemy(1)

enemyindex ++ enemyindex iterate from 1 to 2.
etc.

1 Like

Thanks for trying to help! To. make it easier for users to look at the code, please format the code using </> please

Thanks man!!

``````enemyIndex = 0
strongesthealth = 0

while(enemyIndex < enemies.length)

while(0 is below < number of enemies)
enemy = enemies[enemyIndex] (0)

lets say first monster hp is 10
if 10 is > 0;
strongesthealth become 10.
strongest = enemy (0)

enemyIndex ++
enemyindex iterate from 0 to 1.

while 1 < enemy.length:
enemy = enemies[enemyindex] (1)

lets say enemy1 hp is 20 now.

if 20 (enemy.health) > strongestHealth (10)
yes 20 is > 10.
so strongesthealth become 20
and strongest become enemy(1)

enemyindex ++ enemyindex iterate from 1 to 2.
etc.
``````

help me please I am not moving or saying anything

``````# You have one arrow. Make it count!
# This should return the enemy with the most health.
def findStrongestEnemy(enemies):
strongestHealth = 0
enemyIndex = 0
# While enemyIndex is less than the length of enemies:
while enemyIndex < len(enemies):
# Set an enemy variable to enemies[enemyIndex]
enemyIndex = enemies[enemyIndex]
# If enemy.health is greater than strongestHealth
if enemy.health > strongestHealth:
# Set `strongest` to enemy
# Set strongestHealth to enemy.health
strongest = enemy
strongestHealth = enemy.health
# Increment enemyIndex
enemyIndex += 1

return strongest
enemies = hero.findEnemies()

``````

First of all, the leader part, where you say the leaderâ€™s name should be outside of the function. It wonâ€™t run otherwise.
Also, because itâ€™s outside the function youâ€™ll need to call the function and define leader with it: leader = findSâ€¦etc.

This is the second problem. I think you can probably see what it is.
Danny

1 Like

here is my code:

# This should return the enemy with the most health.

def findStrongestEnemy(enemies):
strongest = None
strongestHealth = 0
enemyIndex = 0
# While enemyIndex is less than the length of enemies:
while enemyIndex < array.length:
# Set an enemy variable to enemies[enemyIndex]
enemies[enemyIndex]
# If enemy.health is greater than strongestHealth
if enemy.health > strongestHealth:
# Set `strongest` to enemy
# Set strongestHealth to enemy.health
strongestHealth = enemy.health
# Increment enemyIndex
enemyIndex += 1

``````return strongest
``````

enemies = hero.findEnemies()

# This should return the enemy with the most health.

def findStrongestEnemy(enemies):
strongest = None
strongestHealth = 0
enemyIndex = 0
# While enemyIndex is less than the length of enemies:
while enemyIndex < array.length:
# Set an enemy variable to enemies[enemyIndex]
enemies[enemyIndex]
# If enemy.health is greater than strongestHealth
if enemy.health > strongestHealth:
# Set `strongest` to enemy
# Set strongestHealth to enemy.health
strongestHealth = enemy.health
# Increment enemyIndex
enemyIndex += 1

``````return strongest
``````

enemies = hero.findEnemies()

There are two mistakes: 1) youâ€™re looping through â€śarrayâ€ť.length. Whatâ€™s array? Why arenâ€™t you using the array you use in the rest of your code.
2)

Youâ€™re only doing the second line. You also have to do the first line.
Also, in future please post your code formatted (with three ``` on a new line before and after your code), because itâ€™s a lot easier for me to read.
Thanks
Danny

1 Like

okay. thank you!!!
iâ€™m doing my homework

iâ€™m talking right now to jake_doggy

// 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;
var enemy = hero.findNearestEnemy();

``````// While enemyIndex is less than the length of enemies:
while (enemyIndex < enemies.length){
// Set an enemy variable to enemies[enemyIndex]
var x = enemies[enemyIndex];
// If enemy.health is greater than strongestHealth
if (enemy.health > strongestHealth){
// Set `strongest` to enemy
strongest = enemy;
// Set strongestHealth to enemy.health
strongestHealth = enemy.health;
}
// Increment enemyIndex
enemyIndex++;

return strongest;
``````

}
}
var enemies = hero.findEnemies();
}

pls help meee!!!

Hi Mita_Nayak,

Delete this line:

``````var enemy = hero.findNearestEnemy();
``````

and set enemy as the variable in this line:

``````var x = enemies[enemyIndex];
``````

Hopefully that works, but if it doesnâ€™t can you repost your code with all of it formatted (using the </> button) please?

Jenny