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

Sarven Shepherd help please Java script

so i can’t get my character to move or attack and i don’t know whats wrong with my code can someone help

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(true) {
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;

}
hero.moveXY(40, 32);

// Between waves, move back to the center.

}Preformatted text

Howdy and welcome to the forum!

Please be sure to post all of your code properly formatted. You can learn how to do so here: [Essentials] How To Post/Format Your Code Correctly

Also, when posting javascript, add the term ‘javascript’, just after the first three backticks. It will look like this:
image

// comments will look like comments
hero.attack(enemy); //will look like code
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(true) {
    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;
}
hero.moveXY(40, 32);

    // Between waves, move back to the center.
    
}

Much better, but you definitely have a problem with indentation…it doesn’t break the code in this case, but it does make it hard to read. There is a button that will automatically line things up for you:
image

I’ve done that for you:

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 (true) {
        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;
    }
    hero.moveXY(40, 32);    // Between waves, move back to the center.
}

The key here is the two comments:

    // Wrap this logic in a while loop to attack all enemies.
    // Find the array's length with:  enemies.length

You currently have this as a second while true…instead, you should make it a conditional while loop. Something like:

    // Wrap this logic in a while loop to attack all enemies.
    // Find the array's length with:  enemies.length
    while (thisVariable < thatVariable.length) {
        // continue your coding here

Finally, your increment statement

        enemyIndex + 1;

is missing something…