Max Maxer Strikes Back(javascript)


#1
// The smaller ogres here do more damage!
// Attack the ogres with the least health first.
while(true) {
    var weakest = null;
    var leastHealth = 99999;
    var enemyIndex = 0;
    var enemies = hero.findEnemies();
    var enemy = enemies[enemyIndex];

    // Loop through all enemies.
    while(enemyIndex < enemies.length){
        // If an enemy's health is less than leastHealth,
        if(enemy.health < leastHealth){
            // make it the weakest and set leastHealth to its health.
            weakest = enemy;
            leastHealth = enemy.health;
        }
    if (weakest) {
        // Attack the weakest ogre.
        hero.attack(weakest);
    }
    enemyIndex +=1;
    }
}

I can’t figure out what is wrong. My hero goes and attacks the weakest, but then stays there.


#2

Your while loop is where you are iterating through the array of enemies. Shouldn’t that be where you increment instead of after the attack code?


#3

I just tried to place the increment there(inside the while loop) there, but it didn’t change anything.

// The smaller ogres here do more damage!
// Attack the ogres with the least health first.
while(true) {
    var weakest = null;
    var leastHealth = 99999;
    var enemyIndex = 0;
    var enemies = hero.findEnemies();
    var enemy = enemies[enemyIndex];
    
    // Loop through all enemies.
    while(enemyIndex < enemies.length){
        // If an enemy's health is less than leastHealth,
        if(enemy.health < leastHealth){
            // make it the weakest and set leastHealth to its health.
            weakest = enemy;
            leastHealth = enemy.health;
            enemyIndex +=1;
        }

    if (weakest) {
        // Attack the weakest ogre.
        hero.attack(weakest);
    }
    }
}


#4

I’m not great with JS but it looks like it’s tabbed in too far. I believe it should line up with if.


#5
// The smaller ogres here do more damage!
// Attack the ogres with the least health first.
while(true) {
    var weakest = null;
    var leastHealth = 99999;
    var enemyIndex = 0;
    var enemies = hero.findEnemies();
    var enemy = enemies[enemyIndex];
    
    // Loop through all enemies.
    while(enemyIndex < enemies.length){
        // If an enemy's health is less than leastHealth,
        if(enemy.health < leastHealth){
            // make it the weakest and set leastHealth to its health.
            weakest = enemy;
            leastHealth = enemy.health;
        enemyIndex += 1;
        }

    if (weakest) {
        // Attack the weakest ogre.
        hero.attack(weakest);
    }
    }
}

Sorry if this isn’t what you meant-- I’m not too sure what you meant by tabbed in. If this is it, it didn’t affect the outcome of the code from before.


#6
    while(condition1){
        // If an enemy's health is less than leastHealth,
        if(condition2){
            // make it the weakest and set leastHealth to its health.
           // some code
        } #  I think this brace is here 
    enemyIndex += 1;
   }
    if (weakest) {
        // Attack the weakest ogre.
        hero.attack(weakest);
    }

#7

Thank you! He finally moved around and killed whatever enemy was the weakest. However, he keeps on dying.

// The smaller ogres here do more damage!
// Attack the ogres with the least health first.
while(true) {
    var weakest = null;
    var leastHealth = 99999;
    var enemyIndex = 0;
    var enemies = hero.findEnemies();
    var enemy = enemies[enemyIndex];
    
    // Loop through all enemies.
    while(enemyIndex < enemies.length){
        // If an enemy's health is less than leastHealth,
        if(enemy.health < leastHealth){
            // make it the weakest and set leastHealth to its health.
            weakest = enemy;
            leastHealth = enemy.health;
            }
        enemyIndex += 1;
        }

        if (weakest) {
        // Attack the weakest ogre.
        hero.attack(weakest);
    
    }
}



#8

You can try

while (weakest && weakest.health > 0){
    hero.attack(weakest)
}

to kill your enemies one by one , use cleave or something else or buy better gear ( buy wisely and if you don’t have the possibility to buy really needed armor or weapon, return to the level later).