[SOLVED] Sheperds let's me attack the yak not the scouts

I can get to defeat the ogres not using the arrays. Since I’m a beginner I would love to learn how to use the arrays. I know it works fine if I were to kill the yaks by changing != to ==. How come it doesn’t attack the rest of the enemy? Thanks.

// Use while loops to pick out the ogre

loop {
var enemies = this.findEnemies();
var enemyIndex = 0;
var enemy = enemies[enemyIndex];
while (enemyIndex < enemies.length) { // Find the array’s length with: enemies.length
if (enemy && enemy.type != “sand-yak”) {
while (enemy.health > 0) {
if (this.isReady(“bash”)) {
this.bash(enemy); }
else if (this.isReady(“cleave”)) {
this.cleave(enemy);}
else {
this.attack(enemy); }
}
// While the enemy’s health is greater than 0, attack it!
}
enemyIndex = 1 + enemyIndex;
}
// Between waves, move back to the center.
if (enemy === 0) {
this.moveXY(40, 32);
}
}

Ok, now I can attack because I changed the index from 0 to 1. But after killing the first two scouts; since the rest take long to appear, I no longer attack anybody…

I’ve fixed something else:
I’ve changed === to !enemy and it goes back to the central X
I still have the problem of not attacking again… Please, help

Hello, tajjeb, and welcome. Please format your code according to the FAQ.

Move the part when you define enemy into the while-loop. The while-loop should look like this:

var enemyIndex = 0;
while (enemyIndex < enemies.length) {
    var enemy = enemies[enemyIndex];
    if (enemy && enemy.type !== "sand-yak") {
        // Do stuff here. 

Sorry I have to read carefully the FAQ.
Thanks for the help. I fixed the code and it worked after adding code to flee from the yaks.
Great

Despite the tips that everyone has mentioned, my hero still doesn’t move. Can someone help me out?

// Use while loops to pick out the ogre

while(true) {
    var enemies = hero.findEnemies();
    var enemyIndex = 0;

    // Wrap this logic in a while loop to attack all enemies.
    // Find the array's length with:  enemies.length
    while (enemies.length) {
        
    
    var enemy = enemies[enemyIndex];
    // "!=" means "not equal to."
    if (enemy.type != "sand-yak") {
        // While the enemy's health is greater than 0, attack it!
        while (enemy.health > 0) {
            hero.attack(enemy);
        }
    }
}
    // Between waves, move back to the center.
    hero.moveXY(40, 32);
}

After this you forgot to increase enemyIndex by one, don’t you think? (It will also stop an infinite loop from happening in some situations too.)

Andrei

1 Like

After I did that the duck shows up again and says "Line 14: TypeError: Cannot read property ‘type’ undefined.

// Use while loops to pick out the ogre

while(true) {
    var enemies = hero.findEnemies();
    var enemyIndex = 0;

    // Wrap this logic in a while loop to attack all enemies.
    // Find the array's length with:  enemies.length
    while (enemies.length) {
        
    
    var enemy = enemies[enemyIndex];
    // "!=" means "not equal to."
    if (enemy.type != "sand-yak") {
        // While the enemy's health is greater than 0, attack it!
        while (enemy.health > 0) {
            hero.attack(enemy);
        }
    }
enemyIndex += 1;
}
    // Between waves, move back to the center.
    hero.moveXY(40, 32);
}

Can you try to post your code again, but this time press the U button from the top of the workspace?

Andrei

Here make a while loop that occurs every time enemyIndex < enemies.length.

Andrei

Thank you my code now works perfectly but what do you mean by next time pressing the U button at the top of the workspace?

1 Like

Do you see the U button that is after the hints button? If so, press it and it will format your code better.

Andrei

image

1 Like

Yes, that is it. Thanks @dedreous!

Andrei

1 Like

OK and thank you! I will use it next time. :smiley:

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.