[Help me] Borrowed sword [Solved]

while (true) {
    var bestTarget = null;
    var enemies = hero.findNearestEnemy();
    var friends = hero.findFriends();
    var enemiesIndex = 0;
    var maxHealth = 99999;
    for (var i = 0; i < friends.length; i++) {
        var friend = friends[i];
    }
    while (enemiesIndex < enemies.length) {
        var enemy = enemies[enemiesIndex];
        if (enemy && enemy.health  < maxHealth) {
            bestTarget = enemy;
            maxHealth = enemy.health;
            enemiesIndex++;
        }
    }
    if (bestTarget) {
        hero.command(friend, "attack", bestTarget);
    } 
}

Please tell me what is the error

1 - 2 code	
3	    var enemies = hero.findNearestEnemy();   // findNearestEnemy or findEnemies?
4 - 5  code
6	    var maxHealth = 99999;  // 99999 or 0 ?
7	    for (var i = 0; i < friends.length; i++) {
8	        var friend = friends[i]; 
9	    }  // loop useless if  "}" is put here, put the "}" after your friend has attackes the bestTarget
10	    while (enemiesIndex < enemies.length) { 
11	        var enemy = enemies[enemiesIndex];
12	        if (enemy && enemy.health  < maxHealth) { // "<" or  ">"
13	            bestTarget = enemy;
14	            maxHealth = enemy.health;
15	            enemiesIndex++; // increment the index outside if clause
16	        }
17	    }
18	     if (bestTarget) {
19	        hero.command(friend, "attack", bestTarget);
20	    } 
21	}
2 Likes

Thanks for your corrections, but while the problem persists, it seems to me that my units are not purposefully shooting at Yeti, I will think about it tomorrow :upside_down_face: :grinning:

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