Need help on CodeCombat- Sarven Shepherd


#1

I’m trying to beat Sarven Shepherd and I’m using JavaScript. Here is my code: loop {
var enemies = this.findEnemies();
var enemyIndex = 0;

    // Wrap this logic in a while loop to attack all enemies.
    // Find the array's length with:  enemies.length
while (enemyIndex < enemies.length) {
    var enemy = enemies[enemyIndex];
    // "!=" means "not equal to."
    if (enemy.type != "sand-yak") {
        while (health > 0) {
        this.attack(enemy);      
        }
        enemyIndex += 1;
    }

    
}
// Between waves, move back to the center.
this.moveXY(40,32);
}

#2

Can you please give a little bit more information? Does it say anything like “error on line 2” or ‘‘fix you code’’?


#3

If enemies[enemyIndex] is sand-yak, you will never get into the if so you will never increment enemyIndex.

You’ll find a sand-yank, see that it is a sand-yak, then go again through the loop and find the same yak again.

Move the enemyIndex+=1; out of the if after the if closing bracket }
By the way add one is such an used structure that it has an even short version:

enemyIndex++;