Brittle Morale - Javascript: I really need help[SOLVED]

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);
}

your indentation is wrong

why does it say this though

send your equipment please

Cheddar, it doesen’t matter what equipment he has

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!

Hi HeroA,

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!

Jenny

1 Like

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);
}

nvm

switch that around (enemy.health and strongestHealth)

also make a variable for strongest to enemy.

make it enemyIndex += 1

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);
}

Hi HeroA,

This is now the third time I’ve posted (you had a previous topic on this), and you still need to change the same three things:

1: This line is wrong:

We’ve given you lots of hints. Look back at Sand Snakes to see how you updated the variable in the while loop there.

  1. This line needs to be switched round. You also need to update the variable strongest as well:
  1. These two lines need swapping over:

Please make all three of these changes. Then your code should work.

Jenny

2 Likes

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);
}

Well done on those.

For 1, look back at what you did for Sand Snakes. It should have looked like this:

    while(coinIndex < coins.length) {
        var coin = coins[coinIndex];

Can you do the same with enemies, enemy and enemyIndex?

For the rest of 2, the comments for this section read:

            // Set `strongest` to enemy
            // Set strongestHealth to enemy.health

Don’t change your strongestHealth one - it’s right at the moment!

Jenny

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);
}

Yup, one more thing! The comment for this line

is

// If enemy.health is greater than strongestHealth

Jenny

2 Likes

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);
}

sorry for the 3 edits, i understand now

see something wrong? enemyIndex += 1; try to find the diffenrence :wink:

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);
}

like i said

20 (chaaaaaaaaaaaaaaaaaaaaars)

2 Likes