# Library Tactician - help

**URL:** <https://discourse.codecombat.com/t/library-tactician-help/27978>\
**Category:** Level Help\
**Created:** [March 17, 2021, 2:21am UTC](https://discourse.codecombat.com/t/library-tactician-help/27978 "2021-03-17T02:21:59Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![help](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@help](https://discourse.codecombat.com/u/help)\
**Post date:** [March 17, 2021, 2:21am UTC](https://discourse.codecombat.com/t/library-tactician-help/27978/1 "2021-03-17T02:21:59Z")

</div>

Archers appear to not target the strongest enemy. Any help is appreciated.

```javascript
var archerTarget = null;

// Soldiers spread out in a circle and defend.
function commandSoldier(soldier, soldierIndex, numSoldiers) {
    var angle = Math.PI * 2 * soldierIndex / numSoldiers;
    var defendPos = {x: 41, y: 40};
    defendPos.x += 10 * Math.cos(angle);
    defendPos.y += 10 * Math.sin(angle);
    hero.command(soldier, "defend", defendPos);
}
// Find the strongest target (most health)
// This function returns something! When you call the function, you will get some value back.
function findStrongestTarget() {
    var mostHealth = 0;
    var bestTarget = null;
    var enemies = hero.findEnemies();
    // Figure out which enemy has the most health, and set bestTarget to be that enemy.
    for(var i=0;i<enemies;i++){
        var enemie = enemies[i];
        if (enemie.health > mostHealth) {
            mostHealth = enemie.health;
            bestTarget = enemie;    
        }
    }
    // Only focus archers' fire if there is a big ogre.
    if (bestTarget && bestTarget.health > 15) {
        return bestTarget;
    } else {
        return null;
    }
}
// If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
function commandArcher(archer) {
    var nearest = archer.findNearestEnemy();
    if(archerTarget) {
        hero.command(archer, "attack", archerTarget);
    } else if(nearest) {
        hero.command(archer, "attack", nearest);
    }
}

while(true) {
    // If archerTarget is defeated or doesn't exist, find a new one.
    if(!archerTarget || archerTarget.health <= 0) {
        // Set archerTarget to be the target that is returned by findStrongestTarget()
        archerTarget = findStrongestTarget();
    }
    var friends = hero.findFriends();
    var soldiers = hero.findByType("soldier");
    // Create a variable containing your archers.
    var archers = hero.findByType("archers");
    for(var i=0; i < soldiers.length; i++) {
        var soldier = soldiers[i];
        commandSoldier(soldier, i, soldiers.length);
    }
    for(var i=0;i<archers.length;i++){
        var archer = archers[i];
        commandArcher(archer);
        }
    // use commandArcher() to command your archers
}

```

---

<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:** [March 17, 2021, 2:09pm UTC](https://discourse.codecombat.com/t/library-tactician-help/27978/2 "2021-03-17T14:09:57Z")

</div>

> [@help](#):
>
> `hero.findByType("archers");`

I haven’t tested this, but I’m pretty sure this is the only error. It’s a typo. It should be “archer”, singular, not “archers”, plural.  
When you use hero.findByType(), you always have to use the .type of the unit, which is always singular.  
Danny

---

<div class="post-metadata">

**Author:** ![goutham123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/goutham123/32/21341_2.png) [@goutham123](https://discourse.codecombat.com/u/goutham123)\
**Post date:** [March 17, 2021, 4:14pm UTC](https://discourse.codecombat.com/t/library-tactician-help/27978/3 "2021-03-17T16:14:25Z")

</div>

I think it might be in your function calling part

```python
commandArcher(archer);

```

is different from the way you called the soldiers function

```python
 commandSoldier(soldier, i, soldiers.length);

```

maybe I think it is in that part

---

<div class="post-metadata">

**Author:** ![help](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@help](https://discourse.codecombat.com/u/help)\
**Post date:** [March 18, 2021, 1:28am UTC](https://discourse.codecombat.com/t/library-tactician-help/27978/4 "2021-03-18T01:28:43Z")

</div>

I believe different parameters are required to execute those two functions.

---

<div class="post-metadata">

**Author:** ![help](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@help](https://discourse.codecombat.com/u/help)\
**Post date:** [March 18, 2021, 1:30am UTC](https://discourse.codecombat.com/t/library-tactician-help/27978/5 "2021-03-18T01:30:52Z")

</div>

It looks like the archer fire is now more concentrated, but still failing the level. Really not sure what’s going on lol

---

<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:** [March 18, 2021, 10:23am UTC](https://discourse.codecombat.com/t/library-tactician-help/27978/6 "2021-03-18T10:23:40Z")

</div>

Please could you post your new code.  
This level is a tricky one. It sometimes acts quite strangely.  
I’ll give your code a test and see if I can fix it.  
Danny

---

<div class="post-metadata">

**Author:** ![goutham123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/goutham123/32/21341_2.png) [@goutham123](https://discourse.codecombat.com/u/goutham123)\
**Post date:** [March 18, 2021, 4:11pm UTC](https://discourse.codecombat.com/t/library-tactician-help/27978/7 "2021-03-18T16:11:31Z")

</div>

No @help The way you are calling the function is wrong. You should do this

```python
commandArcher(archer, i, archer.length);

```

---

<div class="post-metadata">

**Author:** ![goutham123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/goutham123/32/21341_2.png) [@goutham123](https://discourse.codecombat.com/u/goutham123)\
**Post date:** [March 18, 2021, 4:15pm UTC](https://discourse.codecombat.com/t/library-tactician-help/27978/10 "2021-03-18T16:15:37Z")

</div>

@Deadpool198 is right too you need to keep

```python
var archers = hero.findByType("archer");

```

instead of

```python
var archers = hero.findByType("archers");

```

---

<div class="post-metadata">

**Author:** ![help](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@help](https://discourse.codecombat.com/u/help)\
**Post date:** [March 18, 2021, 10:04pm UTC](https://discourse.codecombat.com/t/library-tactician-help/27978/11 "2021-03-18T22:04:48Z")

</div>

```javascript
var archerTarget = null;

// Soldiers spread out in a circle and defend.
function commandSoldier(soldier, soldierIndex, numSoldiers) {
    var angle = Math.PI * 2 * soldierIndex / numSoldiers;
    var defendPos = {x: 41, y: 40};
    defendPos.x += 10 * Math.cos(angle);
    defendPos.y += 10 * Math.sin(angle);
    hero.command(soldier, "defend", defendPos);
}
// Find the strongest target (most health)
// This function returns something! When you call the function, you will get some value back.
function findStrongestTarget() {
    var mostHealth = 0;
    var bestTarget = null;
    var enemies = hero.findEnemies();
    // Figure out which enemy has the most health, and set bestTarget to be that enemy.
    for(var i=0;i<enemies;i++){
        var enemie = enemies[i];
        if (enemie.health > mostHealth) {
            mostHealth = enemie.health;
            bestTarget = enemie;    
        }
    }
    // Only focus archers' fire if there is a big ogre.
    if (bestTarget && bestTarget.health > 15) {
        return bestTarget;
    } else {
        return null;
    }
}
// If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
function commandArcher(archer) {
    var nearest = archer.findNearestEnemy();
    if(archerTarget) {
        hero.command(archer, "attack", archerTarget);
    } else if(nearest) {
        hero.command(archer, "attack", nearest);
    }
}

while(true) {
    // If archerTarget is defeated or doesn't exist, find a new one.
    if(!archerTarget || archerTarget.health <= 0) {
        // Set archerTarget to be the target that is returned by findStrongestTarget()
        archerTarget = findStrongestTarget();
    }
    var friends = hero.findFriends();
    var soldiers = hero.findByType("soldier");
    // Create a variable containing your archers.
    var archers = hero.findByType("archer");
    for(var i=0; i < soldiers.length; i++) {
        var soldier = soldiers[i];
        commandSoldier(soldier, i, soldiers.length);
    }
    for(var i=0;i<archers.length;i++){
        var archer = archers[i];
        commandArcher(archer);
        }
    // use commandArcher() to command your archers
}

```

---

<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:** [March 19, 2021, 10:46am UTC](https://discourse.codecombat.com/t/library-tactician-help/27978/12 "2021-03-19T10:46:54Z")

</div>

Yaay, this is very satisfying.  
I found the error. There’s only one, and it’s on this line:

> [@help](#):
>
> `for(var i=0;i<enemies;i++){`

Remember that enemies is an array, not a number, and you can only compare numbers with \< and \>. How can you access the _length_ of an array? (clue’s in the name).  
If you change that, it will work 😁.  
Danny

---

<div class="post-metadata">

**Author:** ![help](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@help](https://discourse.codecombat.com/u/help)\
**Post date:** [March 20, 2021, 1:35am UTC](https://discourse.codecombat.com/t/library-tactician-help/27978/13 "2021-03-20T01:35:04Z")

</div>

Thats it! such a simple mistake lol. Much thanks for the assist.

---

<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:** [March 20, 2021, 1:35pm UTC](https://discourse.codecombat.com/t/library-tactician-help/27978/14 "2021-03-20T13:35:44Z")

</div>

Closed on March 20, 2021, 1:35pm UTC

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