# \[SOLVED\] Level:borrowed sword help pls

**URL:** https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524
**Category:** Level Help
**Created:** [July 20, 2015, 1:28am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524 "2015-07-20T01:28:59Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![PandaNoms](https://avatars.discourse-cdn.com/v4/letter/p/db5fbb/32.png) [@PandaNoms](https://discourse.codecombat.com/u/PandaNoms)
#### Post date: [July 20, 2015, 1:28am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/1 "2015-07-20T01:28:59Z")

</div>

Hello, I’m doing exactly what the instructions told me to do for the level but my archers just aren’t strong enough for the job. What do I do?

loop{  
var enemies = this.findEnemies();  
var counter = 0;  
var mostHealth = enemies[counter];  
while(counter \< enemies.length){  
if(enemies[counter].health \> mostHealth.health){  
mostHealth = enemies[counter];  
}  
counter++;  
}

```
var friends = this.findFriends();
var i = 0;
while(i < friends.length){
    
    this.command(friends[i],"attack",mostHealth);
i++;
}

```

}

---

<div class="post-metadata">

### Author: ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)
#### Post date: [July 22, 2015, 12:50am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/3 "2015-07-22T00:50:08Z")

</div>

Please format your code as described in the FAQ. This makes it more likely that others will help you as they don’t have to do the formatting by themselves.

* * *

If I see that right I win by using (almost) the same code. I just have a check that `mostHealth` actually exists.

Have you tried to submit again? Submitting gives you a new random seed, possibly resulting in another outcome.

---

<div class="post-metadata">

### Author: ![maxbogus](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/maxbogus/32/4041_2.png) [@maxbogus](https://discourse.codecombat.com/u/maxbogus)
#### Post date: [May 17, 2016, 9:55am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/4 "2016-05-17T09:55:45Z")

</div>

[Please don’t post successful solutions.]

---

<div class="post-metadata">

### Author: ![Ironhead](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ironhead/32/7384_2.png) [@Ironhead](https://discourse.codecombat.com/u/Ironhead)
#### Post date: [July 30, 2017, 10:25pm UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/5 "2017-07-30T22:25:21Z")

</div>

I got it with this code (only partially shown to avoid spoiling the level), even though there was an error.

```python
loop{
....
while(counter < enemies.length){
if(enemies[counter].health > mostHealth.health){
mostHealth = enemies[counter];
}
counter++;
}

var friends = this.findFriends();
var i = 0;
while(i < friends.length && enemies){

    this.command(friends[i],"attack",mostHealth);
i++;
}
}

```

---

<div class="post-metadata">

### Author: ![raven\_axel](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@raven\_axel](https://discourse.codecombat.com/u/raven_axel)
#### Post date: [June 7, 2018, 5:44am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/6 "2018-06-07T05:44:14Z")

</div>

```python
# For this level, your hero doesn't fight.
# Command your archers to focus fire on the enemy with the most health!
while True:
    friend = hero.findFriends()
    enemies = hero.findEnemies()
    highestHp =None 
    bestHealth=0
    for enemy in enemies:
        if enemy.health >= bestHealth:
            highestHp = enemy
            bestHealth = enemy.health
            
    if highestHp:
        friend = hero.findFriends()
        
        hero.command(friend, "attack", highestHp)

```

If someone would be so kind as to tell me how to tell the archers how to attack the highestHp enemy … i get the error that , that is an array … and i’m really really stuck

---

<div class="post-metadata">

### Author: ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)
#### Post date: [June 7, 2018, 9:19am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/7 "2018-06-07T09:19:40Z")

</div>

> [@raven\_axel](#):
>
> friend = hero.findFriends() hero.command(friend, “attack”, highestHp)

It’s because you’re trying to command an array: `friend = hero.findFriends()` even though you’ve defined `friend` instead of `friends` which is a bit confusing. What you need to do is make a for loop and command each individual `friend` in `hero.findFriends()`

---

<div class="post-metadata">

### Author: ![raven\_axel](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@raven\_axel](https://discourse.codecombat.com/u/raven_axel)
#### Post date: [June 8, 2018, 12:54am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/8 "2018-06-08T00:54:45Z")

</div>

Sir I just wanted to say thank you yet again 😃 It worked !

---

<div class="post-metadata">

### Author: ![chichi\_King](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chichi_king/32/8247_2.png) [@chichi\_King](https://discourse.codecombat.com/u/chichi_King)
#### Post date: [June 8, 2018, 7:45am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/9 "2018-06-08T07:45:19Z")

</div>

> [@raven\_axel](#):
>
> while True: friend = hero.findFriends() enemies = hero.findEnemies() highestHp =None bestHealth=0 for enemy in enemies: if enemy.health \>= bestHealth: highestHp = enemy bestHealth = enemy.health if highestHp: friend = hero.findFriends() hero.command(friend, “attack”, highestHp)

```python
while(true) {
  var enemies = hero.findEnemies();
  var friend = hero.findNearest(hero.findFriends());
  var highestHealth = null;
  var health = 0;
for(var i = 0; i < enemies.health; i ++) {
    var enemy = enemies[i];
    if(enemy.health >= health) {
        highestHealth = enemy;
        health = enemy.health;
        if(highestHealth) {
            hero.command(friend, "attack", highestHealth);
       }
    }
} 
}

```

Hey.  
Can you pls tell me what am I doing wrong here?

---

<div class="post-metadata">

### Author: ![jxcwzk](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jxcwzk](https://discourse.codecombat.com/u/jxcwzk)
#### Post date: [February 27, 2019, 12:18pm UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/11 "2019-02-27T12:18:50Z")

</div>

Mod edit: Please do not post final solutions.

---

<div class="post-metadata">

### Author: ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)
#### Post date: [July 12, 2020, 6:04am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/13 "2020-07-12T06:04:35Z")

</div>

Hi @zython and welcome to the forum! 🥳

Is that a working code? If it is, can you delete it because here on the forum we do not want posted solutions? But if you need help at this level, can you format your code as it is described [here](https://discourse.codecombat.com/t/essentials-how-to-post-format-your-code-correctly/15521)?

Andrei

---

<div class="post-metadata">

### Author: ![zython](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zython](https://discourse.codecombat.com/u/zython)
#### Post date: [July 12, 2020, 6:55am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/14 "2020-07-12T06:55:08Z")

</div>

nope, idk ???

---

<div class="post-metadata">

### Author: ![zython](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zython](https://discourse.codecombat.com/u/zython)
#### Post date: [July 12, 2020, 6:56am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/15 "2020-07-12T06:56:34Z")

</div>

I don’t know if that was right or wrong, I just guess.

---

<div class="post-metadata">

### Author: ![zython](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zython](https://discourse.codecombat.com/u/zython)
#### Post date: [July 12, 2020, 7:09am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/16 "2020-07-12T07:09:09Z")

</div>

I think you need to change if(enemy.health \>= health) { to if(enemy.health \> health) { because \>= means equal or greater than. So, it could think that a dead enemy was the greatest.

---

<div class="post-metadata">

### Author: ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)
#### Post date: [July 12, 2020, 7:56am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/17 "2020-07-12T07:56:28Z")

</div>

Please do not revive dead topics unless you have a similar issue.

Andrei

---

<div class="post-metadata">

### Author: ![zython](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zython](https://discourse.codecombat.com/u/zython)
#### Post date: [July 12, 2020, 8:01am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/18 "2020-07-12T08:01:07Z")

</div>

you change for enemy in enemies: to for i in enemies: then, you add enemy = enemies[i]  
and friend = friends[i].

---

<div class="post-metadata">

### Author: ![zython](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zython](https://discourse.codecombat.com/u/zython)
#### Post date: [July 12, 2020, 8:04am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/19 "2020-07-12T08:04:18Z")

</div>

```python
# For this level, your hero doesn't fight.
# Command your archers to focus fire on the enemy with the most health!
while True:
    friend = hero.findFriends()
    enemies = hero.findEnemies()
    best = None 
    maxHealth = 0
    for i in len(enemies):
        enemy = enemies[i]
        friend = friends[i]
        if enemy.health >= bestHealth:
            best = enemy
            maxHealth = enemy.health
            
    if best:
        hero.command(friend, "attack", best)

```

does this work?

---

<div class="post-metadata">

### Author: ![zython](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zython](https://discourse.codecombat.com/u/zython)
#### Post date: [July 12, 2020, 8:09am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/20 "2020-07-12T08:09:01Z")

</div>

I tried this

```python
# For this level, your hero doesn't fight.
# Command your archers to focus fire on the enemy with the most health!
while True:
    friend = hero.findFriends()
    enemies = hero.findEnemies()
    best = None 
    maxHealth = 0
    for i in len(enemies):
        enemy = enemies[i]
        friend = friends[i]
        if enemy.health >= bestHealth:
            best = enemy
            maxHealth = enemy.health
            
    if best:
        hero.command(friend, "attack", best)

```

But it says that it have a error: Need an object. What the heck does that means anyway?

---

<div class="post-metadata">

### Author: ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)
#### Post date: [July 12, 2020, 8:29am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/21 "2020-07-12T08:29:55Z")

</div>

> [@zython](#):
>
> `bestHealth`

What about you maxHealth variable.

> [@zython](#):
>
> `friend = friends[i]`

You can’t command the archers like this. You’ve put the command line outside the for loop. You should create another for loop to command the archers.  
Danny

---

<div class="post-metadata">

### Author: ![zython](https://avatars.discourse-cdn.com/v4/letter/z/65b543/32.png) [@zython](https://discourse.codecombat.com/u/zython)
#### Post date: [July 12, 2020, 8:55am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/22 "2020-07-12T08:55:26Z")

</div>

it still doesn’t work:

```python
# For this level, your hero doesn't fight.
# Command your archers to focus fire on the enemy with the most health!
while True:
    friend = hero.findFriends()
    enemies = hero.findEnemies()
    best = None 
    maxHealth = 0
    for i in len(enemies):
        enemy = enemies[i]
        friend = friends[i]
        if enemy.health >= bestHealth:
            best = enemy
            maxHealth = enemy.health
            
    if best:
        pass

hero.command(friend, "attack", best)

```

---

<div class="post-metadata">

### Author: ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)
#### Post date: [July 12, 2020, 9:05am UTC](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524/24 "2020-07-12T09:05:21Z")

</div>

No, he does not. He said to command each archer to attack individualy.

Andrei

[Next page](https://discourse.codecombat.com/t/solved-level-borrowed-sword-help-pls/4524.md?page=2)
