# Sarven Shepherd (Comp. Sci. 4, JavaScript)

**URL:** <https://discourse.codecombat.com/t/sarven-shepherd-comp-sci-4-javascript/20557>\
**Category:** Level Help\
**Created:** [March 20, 2020, 4:12pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-comp-sci-4-javascript/20557 "2020-03-20T16:12:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Captain\_Critz](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/captain_critz/32/11602_2.png) [@Captain\_Critz](https://discourse.codecombat.com/u/Captain_Critz)\
**Post date:** [March 20, 2020, 4:12pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-comp-sci-4-javascript/20557/1 "2020-03-20T16:12:05Z")

</div>

How do I complete this level? I keep receiving Statement execution limit reached error if I do what it asks of me in the hints. Here is my code.

```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 arrays length with: enemies.length
while(enemyIndex < enemies.length) {
    var enemy = enemies[enemyIndex];
    // "!=" means "not equal to."
    if (enemy.type != "sand-yak") {
        // While the enemys health is greater than 0, attack it!
        if (enemy.health > 0) {
            hero.attack(enemy);
            if (hero.bash.cooldown === 0) {
                hero.bash(enemy);
            }
        }
        enemyIndex += 1;
    }
}
    // Between waves, move back to the center.
}

```

Sorry if it’s messy, I’ve just been stuck here for a while.  
Thanks,  
CaptainCritz.

---

<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:** [March 20, 2020, 6:15pm UTC](https://discourse.codecombat.com/t/sarven-shepherd-comp-sci-4-javascript/20557/2 "2020-03-20T18:15:34Z")

</div>

Howdy and welcome to the forum!

1. Take a closer look at the comment on line 13…it suggests using a different method
2. You are incrementing enemyIndex, but not quite in the correct place…it should be the final statement in your while(enemyIndex…) loop
3. Like item 1, don’t forget the instructions in the comment on line 23

---

<div class="post-metadata">

**Author:** ![brotherofall](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brotherofall/32/11594_2.png) [@brotherofall](https://discourse.codecombat.com/u/brotherofall)\
**Post date:** [March 21, 2020, 12:02am UTC](https://discourse.codecombat.com/t/sarven-shepherd-comp-sci-4-javascript/20557/3 "2020-03-21T00:02:24Z")

</div>

@Captain_Critz

For the most part everything you have done is correct. The problem is in the end of the code. You can remove this:

if (hero.bash.cooldown === 0) {  
hero.bash(enemy);  
}

The hero.attack(enemy) is sufficient.

Don’t forget to close the first if statement before the enemyIndex += 1

The center is hero.moveXY(40, 32); // Remember to include this in the while loop because you will need to go here when no enemies.

Fixed:

```
if (enemy.type != "sand-yak") {
    // While the enemy's health is greater than 0, attack it!
if (enemy.health > 0) {
    hero.attack(enemy);
    }
}
enemyIndex += 1;

```

}  
hero.moveXY(40, 32);  
}
