About a stage of "preferential-treatment"

I need your help! Can someone solve this problem?

// First, loop through all enemies…
loop{
var enemies = this.findEnemies();
var enemyIndex = 0;
var greenflag = this.findFlag(“green”);
var blackflag = this.findFlag(“black”);
var violetflag = this.findFlag(“violet”);

if(greenflag){
this.pickUpFlag(“green”);
} else if(blackflag) {
this.pickUpFlag(“black”);
} else if(violetflag){
this.pickUpFlag(“violet”);
}
// … but only attack ‘thrower’ type enemies.
if(enemies.type == “thrower”){
this.attack(enemies);
this.attack(enemies);
this.attack(enemies);
}
}
// And don’t forget to use your special abilities!

// Then loop through all the enemies again…
loop{
enemies = this.findEnemies();
enemyIndex = 0;
// … and take out everyone who’s still standing.
}

The following is an array of enemies, so you need to specify which enemy you want to check:
var enemies = this.findEnemies();

Let’s make use of that enemyIndex and step through the array of enemies by adding the following:
var enemy = enemies[enemyIndex];

So instead of:
if(enemies.type == “thrower”){
you would use your new enemy within the enemies array:
if(enemy.type == “thrower”){
this.attack(enemy)

Also make sure you add 1 to enemyIndex the end of each loop to make sure you step through all enemies in the array.
++enemyIndex;

Also, how does your loop know when to exit? Instead, consider using a while loop:
while (enemyIndex < enemies.length)

This means that enemy index will have to be defined before the loop starts or else your enemy index will always get reset to 0. Hope this helps!

1 Like