# Sarven Shepherd HELP needed

I have two major questions with this problem:

1. How can I avoid the case in which the computer sees the sand-yak as
its first enemy? For example, every monster in this game(sand-yak and ogres) has its own enemyIndex number. When the computer sees a sand-yak first and gives it an enemyIndex number of 0, I should increment the
value so that my hero attacks the next enemies with higher enemyIndex number. However, when new sand-yaks appear in the game, how can i
differentiate them from other ogres? So I wrote my code like this:

loop {
var enemies = this.findEnemies();
var enemyIndex = 0;
var enemy = enemies[enemyIndex];
while(enemyIndex < enemies.length){
if(enemy && enemy.type == “sand-yak”){
enemyIndex += 1;
while(enemies[enemyIndex].health > 0){
this.attack(enemies[enemyIndex]);
}
}
}
this.moveXY(42, 31);

}

1. This is my second trial of just using enemy instead of enemies[enemyIndex].Why does my code below go through infinite loop?

loop {
var enemies = this.findEnemies();
var enemyIndex = 0;
var enemy = enemies[enemyIndex];
while(enemyIndex < 10){
if(enemy && enemy.type != “sand-yak”){
while(enemy.health > 0){
this.attack(enemy);
}
enemyIndex += 1;
}
}
this.moveXY(42, 31);

}

I formatted the code a little better for you:

loop {
var enemies = this.findEnemies();
var enemyIndex = 0;
var enemy = enemies[enemyIndex];
while(enemyIndex < 10){
if(enemy && enemy.type != "sand-yak"){
while(enemy.health > 0){
this.attack(enemy);
}
enemyIndex += 1;
}
}
this.moveXY(42, 31);
}

Your code does not increase the enemyIndex when the enemy is a sand yak. You need to also get the new enemy based upon the enemyIndex after entering the first while loop. Currently you set the enemy only after entering the “loop” loop which will not change until the next “loop” loop iteration.

Aloso you check the enemyIndex against the magic number “10” which should be replaced by the length of the enemies array.

Later on you should create a filter function which only returns an array of objects which meet a certain condition, like " no yaks", this may work like this:

create an empty output array
iterate over input array
if current element meets condition (for example: type != "sand-yak")
return the output array

There are more options available than this. But keep it simple first

Regarding this:

“every monster in this game(sand-yak and ogres) has its own enemyIndex number”

No, they dont, the enemyIndex number is only the index, the slot it is in the array you iterate, a sand-yak may be index 0 but if you move across the map or kill the yak it may be an other number or even not exist in the returned enemies array.

Marrrk, thank you so much for such a kind and detailed explanation! However, as i run the code you wrote above the result shows that an infinite loop runs on it. I think the computer does not understand the “while(enemyIndex < 10)” part. I am not sure how the findEnemies() function works though. Whether it includes every moving enemy it sees on the screen or not, this problem with infinite loop comes from the while(enemyIndex < 10) part.

Hi there.

I only formatted your code, I didnt change anything besides the formatting.

And I already mentioned why your code might stuck in the endless loop:

Your code does not increase the enemyIndex when the enemy is a sand yak. You need to also get the new enemy based upon the enemyIndex after entering the first while loop. Currently you set the enemy only after entering the “loop” loop which will not change until the next “loop” loop iteration.

I could rephrase is:

1. You need to increment the enemyIndex regardless the condition in your first while loop.
2. You need to assign the variable enemy every time inside the first while loop.