Sarven Shepherd (Comp. Sci. 4, JavaScript)

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.

// 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.

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
1 Like

@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);
}