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

**URL:** <https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337>\
**Category:** Level Help\
**Created:** [January 2, 2021, 8:23pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337 "2021-01-02T20:23:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![HeroA](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/heroa/32/18054_2.png) [@HeroA](https://discourse.codecombat.com/u/HeroA)\
**Post date:** [January 2, 2021, 8:23pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/1 "2021-01-02T20:23:18Z")

</div>

I’m still having trouble with this level. Can you guys please tell me what I’m doing wrong?

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

```

---

<div class="post-metadata">

**Author:** ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)\
**Post date:** [January 2, 2021, 8:50pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/2 "2021-01-02T20:50:28Z")

</div>

> [@HeroA](#):
>
> `enemy.health = strongestHealth;`

your indentation is wrong

> [@HeroA](#):
>
> ` //I did what you said here but it still counts it as an error; it says "Type Error: Can't write to protected property: health". What am I doing wrong?`

why does it say this though

---

<div class="post-metadata">

**Author:** ![cheddarcheese](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cheddarcheese/32/22417_2.png) [@cheddarcheese](https://discourse.codecombat.com/u/cheddarcheese)\
**Post date:** [January 2, 2021, 8:52pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/3 "2021-01-02T20:52:22Z")

</div>

send your equipment please

---

<div class="post-metadata">

**Author:** ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)\
**Post date:** [January 2, 2021, 8:53pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/4 "2021-01-02T20:53:07Z")

</div>

Cheddar, it doesen’t matter what equipment he has

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [January 3, 2021, 1:16am UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/6 "2021-01-03T01:16:41Z")

</div>

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.

```javascript
    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 `stronges`t variable. I suggest you add that line right after assigning the `strongestHealth` variable.

---

<div class="post-metadata">

**Author:** ![HeroA](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/heroa/32/18054_2.png) [@HeroA](https://discourse.codecombat.com/u/HeroA)\
**Post date:** [January 3, 2021, 1:48am UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/7 "2021-01-03T01:48:54Z")

</div>

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:

```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 < 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!

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [January 3, 2021, 8:19am UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/8 "2021-01-03T08:19:07Z")

</div>

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:

> [@HeroA](#):
>
> ```python
> return strongest;
> }
> 
> ```

Try making changes, post your new code if you want more help, and we’ll keep trying to give hints!

Jenny

---

<div class="post-metadata">

**Author:** ![HeroA](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/heroa/32/18054_2.png) [@HeroA](https://discourse.codecombat.com/u/HeroA)\
**Post date:** [January 3, 2021, 11:30pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/9 "2021-01-03T23:30:35Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![WaWa\_Yang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wawa_yang/32/17800_2.png) [@WaWa\_Yang](https://discourse.codecombat.com/u/WaWa_Yang)\
**Post date:** [January 3, 2021, 11:43pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/10 "2021-01-03T23:43:18Z")

</div>

nvm

---

<div class="post-metadata">

**Author:** ![WaWa\_Yang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wawa_yang/32/17800_2.png) [@WaWa\_Yang](https://discourse.codecombat.com/u/WaWa_Yang)\
**Post date:** [January 3, 2021, 11:46pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/11 "2021-01-03T23:46:36Z")

</div>

> [@HeroA](#):
>
> `enemy.health = strongestHealth;`

switch that around (`enemy.health and strongestHealth`)

also make a variable for strongest to enemy.

> [@HeroA](#):
>
> `enemyIndex++`

make it `enemyIndex += 1`

---

<div class="post-metadata">

**Author:** ![HeroA](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/heroa/32/18054_2.png) [@HeroA](https://discourse.codecombat.com/u/HeroA)\
**Post date:** [January 5, 2021, 12:38am UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/12 "2021-01-05T00:38:36Z")

</div>

I did that, but now it still says that. Is there anything else I’m doing wrong? Here is my code:

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

```

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [January 5, 2021, 8:41am UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/13 "2021-01-05T08:41:52Z")

</div>

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:

> [@HeroA](#):
>
> `var enemy = enemyIndex[0]`

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:

> [@HeroA](#):
>
> `enemy.health = strongestHealth;`

1. These two lines need swapping over:

> [@HeroA](#):
>
> ```python
> return strongest;
> }
> 
> ```

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

Jenny

---

<div class="post-metadata">

**Author:** ![HeroA](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/heroa/32/18054_2.png) [@HeroA](https://discourse.codecombat.com/u/HeroA)\
**Post date:** [January 5, 2021, 10:28pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/14 "2021-01-05T22:28:16Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [January 6, 2021, 8:33am UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/15 "2021-01-06T08:33:54Z")

</div>

Well done on those.

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

```python
    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:

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

```

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

Jenny

---

<div class="post-metadata">

**Author:** ![HeroA](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/heroa/32/18054_2.png) [@HeroA](https://discourse.codecombat.com/u/HeroA)\
**Post date:** [January 6, 2021, 3:48pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/16 "2021-01-06T15:48:24Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [January 6, 2021, 5:29pm UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/17 "2021-01-06T17:29:28Z")

</div>

Yup, one more thing! The comment for this line

> [@HeroA](#):
>
> `if (strongestHealth > enemy.health) {`

is

```python
// If enemy.health is greater than strongestHealth

```

Jenny

---

<div class="post-metadata">

**Author:** ![HeroA](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/heroa/32/18054_2.png) [@HeroA](https://discourse.codecombat.com/u/HeroA)\
**Post date:** [January 7, 2021, 1:32am UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/18 "2021-01-07T01:32:43Z")

</div>

Ok, I did that, but my code still doesn’t work. Here is my code:

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

```

---

<div class="post-metadata">

**Author:** ![WaWa\_Yang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wawa_yang/32/17800_2.png) [@WaWa\_Yang](https://discourse.codecombat.com/u/WaWa_Yang)\
**Post date:** [January 7, 2021, 1:56am UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/19 "2021-01-07T01:56:17Z")

</div>

sorry for the 3 edits, i understand now

> [@HeroA](#):
>
> `enemyIndex =+ 1;`

see something wrong? `enemyIndex += 1;` try to find the diffenrence 😉

---

<div class="post-metadata">

**Author:** ![HeroA](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/heroa/32/18054_2.png) [@HeroA](https://discourse.codecombat.com/u/HeroA)\
**Post date:** [January 7, 2021, 2:08am UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/20 "2021-01-07T02:08:19Z")

</div>

I reversed it, but it still doesn’t work. Here is my code after:

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

```

---

<div class="post-metadata">

**Author:** ![WaWa\_Yang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wawa_yang/32/17800_2.png) [@WaWa\_Yang](https://discourse.codecombat.com/u/WaWa_Yang)\
**Post date:** [January 7, 2021, 2:09am UTC](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337/21 "2021-01-07T02:09:22Z")

</div>

like i said

> [@WaWa\_Yang](#):
>
> sorry for the 3 edits, i understand now
> 
> > [@HeroA](#):
> >
> > `enemyIndex =+ 1;`
> 
> see something wrong? `enemyIndex += 1;` try to find the diffenrence 😉

20 (chaaaaaaaaaaaaaaaaaaaaars)

[Next page](https://discourse.codecombat.com/t/brittle-morale-javascript-i-really-need-help-solved/26337.md?page=2)
