# \[Help me\] Borrowed sword \[Solved\]

**URL:** https://discourse.codecombat.com/t/help-me-borrowed-sword-solved/24844
**Category:** Level Help
**Created:** [October 19, 2020, 7:09pm UTC](https://discourse.codecombat.com/t/help-me-borrowed-sword-solved/24844 "2020-10-19T19:09:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Vse\_Putym](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@Vse\_Putym](https://discourse.codecombat.com/u/Vse_Putym)
#### Post date: [October 19, 2020, 7:09pm UTC](https://discourse.codecombat.com/t/help-me-borrowed-sword-solved/24844/1 "2020-10-19T19:09:16Z")

</div>

```python
while (true) {
    var bestTarget = null;
    var enemies = hero.findNearestEnemy();
    var friends = hero.findFriends();
    var enemiesIndex = 0;
    var maxHealth = 99999;
    for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];
    }
    while (enemiesIndex < enemies.length) {
        var enemy = enemies[enemiesIndex];
        if (enemy && enemy.health < maxHealth) {
            bestTarget = enemy;
            maxHealth = enemy.health;
            enemiesIndex++;
        }
    }
    if (bestTarget) {
        hero.command(friend, "attack", bestTarget);
    } 
}

```

Please tell me what is the error

---

<div class="post-metadata">

### Author: ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)
#### Post date: [October 19, 2020, 8:03pm UTC](https://discourse.codecombat.com/t/help-me-borrowed-sword-solved/24844/2 "2020-10-19T20:03:38Z")

</div>

```python
1 - 2 code	
3 var enemies = hero.findNearestEnemy(); // findNearestEnemy or findEnemies?
4 - 5 code
6 var maxHealth = 99999; // 99999 or 0 ?
7 for (var i = 0; i < friends.length; i++) {
8 var friend = friends[i]; 
9 } // loop useless if "}" is put here, put the "}" after your friend has attackes the bestTarget
10 while (enemiesIndex < enemies.length) { 
11 var enemy = enemies[enemiesIndex];
12 if (enemy && enemy.health < maxHealth) { // "<" or ">"
13 bestTarget = enemy;
14 maxHealth = enemy.health;
15 enemiesIndex++; // increment the index outside if clause
16 }
17 }
18 if (bestTarget) {
19 hero.command(friend, "attack", bestTarget);
20 } 
21	}
```

---

<div class="post-metadata">

### Author: ![Vse\_Putym](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@Vse\_Putym](https://discourse.codecombat.com/u/Vse_Putym)
#### Post date: [October 19, 2020, 8:41pm UTC](https://discourse.codecombat.com/t/help-me-borrowed-sword-solved/24844/3 "2020-10-19T20:41:07Z")

</div>

Thanks for your corrections, but while the problem persists, it seems to me that my units are not purposefully shooting at Yeti, I will think about it tomorrow 🙃 😀

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)
#### Post date: [October 20, 2020, 8:41am UTC](https://discourse.codecombat.com/t/help-me-borrowed-sword-solved/24844/4 "2020-10-20T08:41:10Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
