# Level Toil and Trouble

**URL:** <https://discourse.codecombat.com/t/level-toil-and-trouble/4889>\
**Category:** Uncategorized\
**Created:** [September 10, 2015, 4:49pm UTC](https://discourse.codecombat.com/t/level-toil-and-trouble/4889 "2015-09-10T16:49:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![yodas](https://avatars.discourse-cdn.com/v4/letter/y/e274bd/32.png) [@yodas](https://discourse.codecombat.com/u/yodas)\
**Post date:** [September 10, 2015, 4:49pm UTC](https://discourse.codecombat.com/t/level-toil-and-trouble/4889/1 "2015-09-10T16:49:58Z")

</div>

can anyone help me by this level?

here is my code:

```
this.chooseTarget = function(friend) {
    if (friend == "soldier") {
        return this.findByType("witch");
    }
    else {
        return this.findEnemies(); 
    }
};    
loop {
    var friends = this.findFriends();
    for(i=0; i < friends.length; i++) {            
        var friend = friends[i];
        var enemy = friend.findNearest(this.chooseTarget(friend.type));
        if (enemy) {
            this.command(friend, "attack", enemy);
        }       
    }
}
```

---

<div class="post-metadata">

**Author:** ![GundericusTheMighty](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@GundericusTheMighty](https://discourse.codecombat.com/u/GundericusTheMighty)\
**Post date:** [September 10, 2015, 10:22pm UTC](https://discourse.codecombat.com/t/level-toil-and-trouble/4889/2 "2015-09-10T22:22:36Z")

</div>

Try `friend.type` instead of “friend” in line 2.

---

<div class="post-metadata">

**Author:** ![yodas](https://avatars.discourse-cdn.com/v4/letter/y/e274bd/32.png) [@yodas](https://discourse.codecombat.com/u/yodas)\
**Post date:** [September 11, 2015, 11:21am UTC](https://discourse.codecombat.com/t/level-toil-and-trouble/4889/3 "2015-09-11T11:21:09Z")

</div>

the Qode works  
but i dont win the level

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nick/32/784_2.png) [@nick](https://discourse.codecombat.com/u/nick)\
**Post date:** [September 11, 2015, 7:20pm UTC](https://discourse.codecombat.com/t/level-toil-and-trouble/4889/4 "2015-09-11T19:20:44Z")

</div>

Should work with that change; what ends up happening?

---

<div class="post-metadata">

**Author:** ![yodas](https://avatars.discourse-cdn.com/v4/letter/y/e274bd/32.png) [@yodas](https://discourse.codecombat.com/u/yodas)\
**Post date:** [September 12, 2015, 10:07am UTC](https://discourse.codecombat.com/t/level-toil-and-trouble/4889/5 "2015-09-12T10:07:36Z")

</div>

at the end not all ogres die  
I dont have an idea what I can do  
please help me

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nick/32/784_2.png) [@nick](https://discourse.codecombat.com/u/nick)\
**Post date:** [September 12, 2015, 11:52am UTC](https://discourse.codecombat.com/t/level-toil-and-trouble/4889/6 "2015-09-12T11:52:23Z")

</div>

Your guys die before killing them? Do the soldiers go straight for the witches, while the archers shoot the nearest ogres to themselves? Or do the soldiers attack the nearest ogres, too?

Remember you have to make the change GundericusTheMighty suggested.

---

<div class="post-metadata">

**Author:** ![yodas](https://avatars.discourse-cdn.com/v4/letter/y/e274bd/32.png) [@yodas](https://discourse.codecombat.com/u/yodas)\
**Post date:** [September 12, 2015, 1:29pm UTC](https://discourse.codecombat.com/t/level-toil-and-trouble/4889/7 "2015-09-12T13:29:10Z")

</div>

yes my guys die before  
the soldiers attack only the witches and the archers attack the nearest orgres

---

<div class="post-metadata">

**Author:** ![GundericusTheMighty](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@GundericusTheMighty](https://discourse.codecombat.com/u/GundericusTheMighty)\
**Post date:** [September 12, 2015, 6:46pm UTC](https://discourse.codecombat.com/t/level-toil-and-trouble/4889/8 "2015-09-12T18:46:56Z")

</div>

What are the archers attacking? They should be going after the throwers first.

---

<div class="post-metadata">

**Author:** ![trotod](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/trotod/32/3263_2.png) [@trotod](https://discourse.codecombat.com/u/trotod)\
**Post date:** [September 12, 2015, 10:40pm UTC](https://discourse.codecombat.com/t/level-toil-and-trouble/4889/9 "2015-09-12T22:40:07Z")

</div>

Depending on what glasses you have equipped for some reason you can’t always see the bottom throwers. Your friends can, so you could replace `this.findEnemies()` with `friend.findEnemies()`.

---

<div class="post-metadata">

**Author:** ![yodas](https://avatars.discourse-cdn.com/v4/letter/y/e274bd/32.png) [@yodas](https://discourse.codecombat.com/u/yodas)\
**Post date:** [September 13, 2015, 9:09am UTC](https://discourse.codecombat.com/t/level-toil-and-trouble/4889/10 "2015-09-13T09:09:27Z")

</div>

thank you very much trotod  
now it works 😄
