# Brittle Morale \[JavaScript\]

**URL:** <https://discourse.codecombat.com/t/brittle-morale-javascript/21212>\
**Category:** Level Help\
**Created:** [April 26, 2020, 3:16pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript/21212 "2020-04-26T15:16:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Captain\_Critz](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/captain_critz/32/11602_2.png) [@Captain\_Critz](https://discourse.codecombat.com/u/Captain_Critz)\
**Post date:** [April 26, 2020, 3:16pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript/21212/1 "2020-04-26T15:16:48Z")

</div>

Does anyone know what is wrong with the following code?

My character just stands there without an error sign

_menacingly_

```python
// 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 (enemies > enemyIndex) {
        // Set an enemy variable to enemies[enemyIndex]
        var enemy = 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 ++;
    }
    return strongest;
}

var enemies = hero.findEnemies();
var leader = findStrongestEnemy(enemies);
if (leader) {
    hero.say(leader);
}

```

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [April 26, 2020, 3:24pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript/21212/2 "2020-04-26T15:24:54Z")

</div>

Have a closer look at the comment:

```python
    // While enemyIndex is less than the length of enemies:

```

---

<div class="post-metadata">

**Author:** ![Captain\_Critz](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/captain_critz/32/11602_2.png) [@Captain\_Critz](https://discourse.codecombat.com/u/Captain_Critz)\
**Post date:** [April 26, 2020, 3:36pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript/21212/3 "2020-04-26T15:36:20Z")

</div>

I made the change but no dice. Any other things you might see that could be causing this?

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [April 26, 2020, 3:43pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript/21212/4 "2020-04-26T15:43:00Z")

</div>

There were actually two issues with your while statement… **less than** and **length**

Actually, I just tested with your code and the order you have the statements will work with \>. Post your while statement again please.

---

<div class="post-metadata">

**Author:** ![Captain\_Critz](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/captain_critz/32/11602_2.png) [@Captain\_Critz](https://discourse.codecombat.com/u/Captain_Critz)\
**Post date:** [April 26, 2020, 3:47pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript/21212/5 "2020-04-26T15:47:15Z")

</div>

Matters no more, as the length was the problem. Thanks!

---

<div class="post-metadata">

**Author:** ![Mita\_Nayak](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/mita_nayak/32/15668_2.png) [@Mita\_Nayak](https://discourse.codecombat.com/u/Mita_Nayak)\
**Post date:** [October 19, 2020, 9:39pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript/21212/6 "2020-10-19T21:39:31Z")

</div>

help me pls.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;  
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);  
}  
I kill the leader, but the hero will die and then say that I failed.
