# \[SOLVED\] Sheperds let's me attack the yak not the scouts

**URL:** <https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122>\
**Category:** Level Help\
**Created:** [October 3, 2015, 1:04pm UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122 "2015-10-03T13:04:05Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![tajjeb](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@tajjeb](https://discourse.codecombat.com/u/tajjeb)\
**Post date:** [October 3, 2015, 1:04pm UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/1 "2015-10-03T13:04:05Z")

</div>

I can get to defeat the ogres not using the arrays. Since I’m a beginner I would love to learn how to use the arrays. I know it works fine if I were to kill the yaks by changing != to ==. How come it doesn’t attack the rest of the enemy? Thanks.

// Use while loops to pick out the ogre

loop {  
var enemies = this.findEnemies();  
var enemyIndex = 0;  
var enemy = enemies[enemyIndex];  
while (enemyIndex \< enemies.length) { // Find the array’s length with: enemies.length  
if (enemy && enemy.type != “sand-yak”) {  
while (enemy.health \> 0) {  
if (this.isReady(“bash”)) {  
this.bash(enemy); }  
else if (this.isReady(“cleave”)) {  
this.cleave(enemy);}  
else {  
this.attack(enemy); }  
}  
// While the enemy’s health is greater than 0, attack it!  
}  
enemyIndex = 1 + enemyIndex;  
}  
// Between waves, move back to the center.  
if (enemy === 0) {  
this.moveXY(40, 32);  
}  
}

---

<div class="post-metadata">

**Author:** ![tajjeb](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@tajjeb](https://discourse.codecombat.com/u/tajjeb)\
**Post date:** [October 3, 2015, 1:13pm UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/2 "2015-10-03T13:13:33Z")

</div>

Ok, now I can attack because I changed the index from 0 to 1. But after killing the first two scouts; since the rest take long to appear, I no longer attack anybody…

---

<div class="post-metadata">

**Author:** ![tajjeb](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@tajjeb](https://discourse.codecombat.com/u/tajjeb)\
**Post date:** [October 3, 2015, 1:18pm UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/3 "2015-10-03T13:18:42Z")

</div>

I’ve fixed something else:  
I’ve changed === to !enemy and it goes back to the central X  
I still have the problem of not attacking again… Please, help

---

<div class="post-metadata">

**Author:** ![ChronistGilver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chronistgilver/32/3778_2.png) [@ChronistGilver](https://discourse.codecombat.com/u/ChronistGilver)\
**Post date:** [October 3, 2015, 2:23pm UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/4 "2015-10-03T14:23:46Z")

</div>

Hello, tajjeb, and welcome. Please format your code according to the FAQ.

Move the part when you define `enemy` into the while-loop. The while-loop should look like this:

```python
var enemyIndex = 0;
while (enemyIndex < enemies.length) {
    var enemy = enemies[enemyIndex];
    if (enemy && enemy.type !== "sand-yak") {
        // Do stuff here. 

```

---

<div class="post-metadata">

**Author:** ![tajjeb](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@tajjeb](https://discourse.codecombat.com/u/tajjeb)\
**Post date:** [October 3, 2015, 4:19pm UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/5 "2015-10-03T16:19:00Z")

</div>

Sorry I have to read carefully the FAQ.  
Thanks for the help. I fixed the code and it worked after adding code to flee from the yaks.  
Great

---

<div class="post-metadata">

**Author:** ![mitboy3](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/mitboy3/32/13246_2.png) [@mitboy3](https://discourse.codecombat.com/u/mitboy3)\
**Post date:** [July 18, 2020, 7:34pm UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/6 "2020-07-18T19:34:22Z")

</div>

Despite the tips that everyone has mentioned, my hero still doesn’t move. Can someone help me out?

```python
// Use while loops to pick out the ogre

while(true) {
    var enemies = hero.findEnemies();
    var enemyIndex = 0;

    // Wrap this logic in a while loop to attack all enemies.
    // Find the array's length with: enemies.length
    while (enemies.length) {
        
    
    var enemy = enemies[enemyIndex];
    // "!=" means "not equal to."
    if (enemy.type != "sand-yak") {
        // While the enemy's health is greater than 0, attack it!
        while (enemy.health > 0) {
            hero.attack(enemy);
        }
    }
}
    // Between waves, move back to the center.
    hero.moveXY(40, 32);
}

```

---

<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 18, 2020, 8:01pm UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/7 "2020-07-18T20:01:26Z")

</div>

> [@mitboy3](#):
>
> ```python
> if (enemy.type != "sand-yak") {
> // While the enemy's health is greater than 0, attack it!
> while (enemy.health > 0) {
> hero.attack(enemy);
> }
> }
> 
> ```

After this you forgot to increase enemyIndex by one, don’t you think? (It will also stop an infinite loop from happening in some situations too.)

Andrei

---

<div class="post-metadata">

**Author:** ![mitboy3](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/mitboy3/32/13246_2.png) [@mitboy3](https://discourse.codecombat.com/u/mitboy3)\
**Post date:** [July 19, 2020, 4:17am UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/8 "2020-07-19T04:17:47Z")

</div>

After I did that the duck shows up again and says "Line 14: TypeError: Cannot read property ‘type’ undefined.

```python
// Use while loops to pick out the ogre

while(true) {
    var enemies = hero.findEnemies();
    var enemyIndex = 0;

    // Wrap this logic in a while loop to attack all enemies.
    // Find the array's length with: enemies.length
    while (enemies.length) {
        
    
    var enemy = enemies[enemyIndex];
    // "!=" means "not equal to."
    if (enemy.type != "sand-yak") {
        // While the enemy's health is greater than 0, attack it!
        while (enemy.health > 0) {
            hero.attack(enemy);
        }
    }
enemyIndex += 1;
}
    // Between waves, move back to the center.
    hero.moveXY(40, 32);
}

```

---

<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 19, 2020, 5:14am UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/9 "2020-07-19T05:14:21Z")

</div>

> [@mitboy3](#):
>
> `enemy.type != "sand-yak"`

Can you try to post your code again, but this time press the U button from the top of the workspace?

Andrei

---

<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 19, 2020, 5:16am UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/10 "2020-07-19T05:16:34Z")

</div>

> [@mitboy3](#):
>
> `(enemies.length)`

Here make a while loop that occurs every time enemyIndex \< enemies.length.

Andrei

---

<div class="post-metadata">

**Author:** ![mitboy3](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/mitboy3/32/13246_2.png) [@mitboy3](https://discourse.codecombat.com/u/mitboy3)\
**Post date:** [July 19, 2020, 5:13pm UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/11 "2020-07-19T17:13:20Z")

</div>

Thank you my code now works perfectly but what do you mean by next time pressing the U button at the top of the workspace?

---

<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 19, 2020, 5:20pm UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/12 "2020-07-19T17:20:57Z")

</div>

Do you see the U button that is after the hints button? If so, press it and it will format your code better.

Andrei

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [July 19, 2020, 5:21pm UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/13 "2020-07-19T17:21:31Z")

</div>

![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/c/c7ba3ca882b82dd47c39714ee4c99ad13eef95ac.png)

---

<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 19, 2020, 5:22pm UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/14 "2020-07-19T17:22:17Z")

</div>

Yes, that is it. Thanks @dedreous!

Andrei

---

<div class="post-metadata">

**Author:** ![mitboy3](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/mitboy3/32/13246_2.png) [@mitboy3](https://discourse.codecombat.com/u/mitboy3)\
**Post date:** [July 19, 2020, 5:42pm UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/15 "2020-07-19T17:42:05Z")

</div>

OK and thank you! I will use it next time. 😃

---

<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:** [July 20, 2020, 5:42am UTC](https://discourse.codecombat.com/t/solved-sheperds-lets-me-attack-the-yak-not-the-scouts/5122/16 "2020-07-20T05:42:10Z")

</div>

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