Problem with Preferential Treatment level

need help. am I doing it right? the second loop seems problem. I don’t know what it is. -_-’

// First, loop through all enemies...

var enemies = hero.findEnemies();
var enemyIndex = 0;
// ... but only attack "thrower" type enemies.
while (true) {
    
    var enemy = enemies[enemyIndex];
    if(enemy) {
        if(enemy.type === "thrower") {
            hero.attack(enemy);
        }
    }
    enemyIndex +=1;
}

// Then loop through all the enemies again...
enemies = hero.findEnemies();
enemyIndex = 0;
// ... and defeat everyone who's still standing.

  while ( enemyIndex < enemies.length ) {
    enemy = enemies[enemyIndex];
    
    while(enemy.health > 0) {
     hero.attack(enemy);
        
    }
    

    enemyIndex +=1;
    
}

2 Likes

Problem with indentation
This is what it should be.

enemies = hero.findEnemies();
enemyIndex = 0;
while ( enemyIndex < enemies.length ) {
    enemy = enemies[enemyIndex];
    while (enemy.health > 0) {
        hero.attack(enemy);
    }
    enemyIndex += 1;
}

Do same to other bit of code and define enemyIndex and enemies at the top.

1 Like

thanks a lot. Succeed :smiley:

a mistake at while(true)

2 Likes