Problem in Sarven Shepherd (Javascript)

First of all, please paste your code between 3 backticks (`) according to the FAQ so we can see it clearly. Indenting also helps (I :heart: python). You can edit edit your post with the little pencil icon.


What I can see (after cleaning up the formatting) is that you only increase the enemyIndex if the enemy is not a sand yak, because it’s included in the if block. Thus, as soon as you see a sand yak, the while loop becomes an infinite loop, because you don’t increase the index… It’s best to increase the index right after you used it. See the comments below:

loop {
    var enemies = this.findEnemies();
    var enemyIndex = 0;
    
    while (enemyIndex < 3) {
        var enemy = enemies[enemyIndex];
        // increase your index here:
        enemyIndex++;

        if (enemy.type != "sand-yak") {
            while (enemy.health > 0) {
                this.attack(enemy);
            } // end while

            // this is still within the 'if' block:
            // enemyIndex++;

        } //end if

    } // end while

} // end loop

Also, you’re checking only the first 3 enemies:

while (enemyIndex < 3) {

I guess you should check all enemies:

while (enemyIndex < enemies.length) {

Cheers

4 Likes