Brittle morale. Please explain

// 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();
var leader = findStrongestEnemy(enemies);
if (leader) {
    hero.say(leader);
}

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):
    strongest = leader
    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()
    if leader:
        leader = findStrongestEnemy(enemies)
        hero.say(leader)

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

my hero doesn’t do anything. can you please help?
here is my code:

You have one arrow. Make it count!

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()
leader = findStrongestEnemy(enemies)
if leader:
hero.say(leader)

You have one arrow. Make it count!

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()
leader = findStrongestEnemy(enemies)
if leader:
hero.say(leader)

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();
var leader = findStrongestEnemy(enemies);
if (leader) {
hero.say(leader);
}

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