Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Help in sarven shepard(javascript)

Please help me.
I can’t understand the place of enemyIndex++;

while (true) {
    var enemies = hero.findEnemies();
    var enemyIndex = 0;
    while (enemyIndex < enemies.length) {    
        var enemy = enemies[enemyIndex];        
        if (enemy.type != "sand-yak") {
        // While the enemy's health is greater than 0, attack it!
        while (enemy.health > 0) {
            hero.attack(enemy); 
        }
        enemyIndex++;
    }
}
    hero.moveXY(40, 32);
}

Right now your enemyIndex++ is in the if statement so it won’t index the enemy if it is a sand-yak and you get an infinite loop. Move it after the next curly brace to be outside the if statement, but within the while loop. if you put your cursor next to a curly brace, it will highlight the paired curly brace to help show you what is contained within that code. While indenting isn’t needed in javascript it does help keep track of the sections of code better.

while (index < enemies){
    if{
        while{}
    } // end of if statement
    index++
} // end of while loop

Thank you! Now it works :blush: