Borrowed sword. What's wrong?

Hello!
What’s wrong? Determines the strongest enemy, but not enough to kill all the Yeti.

https://codecombat.com/play/level/borrowed-sword?

while(true) {
  var enemies = hero.findEnemies();
  var friends = hero.findFriends();
  var highestHealth = null;
  var health = 0;     
    for(var i = 0; i < enemies.length; i ++) {
        var enemy = enemies[i]; }
            if(enemy.health >= health) {
            highestHealth = enemy;
            health = enemy.health; 
                for(var i = 0; i < friends.length; i ++) {
                var friend = friends[i]; 
                    if(highestHealth) {
                        hero.command(friend, "attack", highestHealth);           
                                                 }   
                                        }
                                }
                        }

Accidentally deleted my own code and rewrote the level following your logic :slight_smile:
Don’t use beatify button, the code becomes ugly-). You have several curly braces put wrongly.
Define function findStrongestEnemy() and call it inside the main while (true) loop

// code
// you can call  the function here
for(var i = 0; i < friends.length; i ++) { // or call the function here + additional code here
}

in both cases of calling the function you will pass the level

1 Like

Please help me))) function looks like this?

function findStrongestEnemy () {
 var enemies = hero.findEnemies();  
 var highestHealth = null; 
 var health = 0;   
    for(var i = 0; i < enemies.length; i ++) {
        var enemy = enemies[i]; 
            if(enemy.health >= health) {
            highestHealth = enemy;
            health = enemy.health; }
    }
}

Your function doesn’t return a value…

I’m completely confused.

function findStrongestEnemy () {
    var strongest = null;
    var enemyIndex = 0;
    var enemies = hero.findEnemies();
    while(enemyIndex < enemies.length) {
        var enemy = enemies[enemyIndex];
        if (enemy.health > enemies[enemyIndex].health) {
            strongest = enemy.health;
        enemyIndex++; 
        }
    }
}

I think the first function is OK except a missing line:

return highestHealth;

and then you call it from main loop with:

var target = findStrongestEnemy();

The second function isn’t right and put return strongest

function findStrongestEnemy () {
    var strongest = null;
    var health = 0;  // you have forgotten this variable
    var enemyIndex = 0;
    var enemies = hero.findEnemies();
    while(enemyIndex < enemies.length) {
        var enemy = enemies[enemyIndex];
        if (enemy.health > enemies[enemyIndex].health) {
            strongest = enemy; // strongest = enemy.health; - wrong
            health  = enemy.health; 
        }  
        enemyIndex++; // incrementing index outside if()
    }
   }
1 Like

Thank you so much!!! Victory)

1 Like

where am i wrong here ,i cannot figure it out

while(true){
    var best=null;//no value
    var i=0;//value
    var heal=0;
    var evils=hero.findEnemies();
    
    while(i<evils.length){
        var evil=evils[i];
        if(evil.heal>evils[i].heal){
            best=evil;
            heal=evil.heal;  
         
            }
          i++;
     return best;        
        }

Hi, one problem here is the evil.heal, it should be evil.health. It would also make sense to change your variable heal to health. You also need to compare the evil.health with the variable heal rather than evils[i].health. The last thing is you need to attack the best, not return it. You only use return in a function.
Your indentations are also a bit off.

while(true){
    var best=null;
    var i=0;
    var heal=0; // maybe "health" might be a more fitting name, as it is the name of the actual property- 
//  -(enemy.health).
    var evils=hero.findEnemies();
    
    while(i<evils.length){
        var evil=evils[i];
        if(evil.heal>evils[i].heal){ // This line has two problems:
//      1) The health property of people and things is ".health" not ".heal".  e.g. hero.health/enemy.health
//      2) If you think about the line, It's actually comparing "evil"'s health with evils[i]'s health,-
//      -when evils[i] is the same as "evil" because on the line above you just defined "evil" as evil[i].
            best=evil;
            heal=evil.heal;  
        }
        i++;
    }
    return best; // Because your code isn't a function you don't need to return it.
//  Instead you need to see if the best exists and, if so, command all your archers (hero.findFriends()) to-
//  -attack it
}

I hope this helps.

1 Like

ive attempted what your recommended,its still not working

while(true){
    var best=null; //no value
    var i=0; //value
    var health=0;
    var evils=hero.findEnemies();
    
    while(i<evils.length){
         var evil=evils[i];
         if(evil.health>health){
         var best=evil; //null 
         var heal=evil.health;   
         }
         i++;
                
         }
         if(best){
             var buds=hero.findFriends();     
             hero.command(buds, "attack", best);  
         } 
        
         
        
}

Hi, you need to use a for-loop to loop through your friends. You can’t command an array, you have to command your friends individually.

still not for some reason i havent figured out myself. :confused:

while(true){
    var best=null; //no value
    var i=0; //value
    var health=0;
    var evils=hero.findEnemies();
    
    while(i<evils.length){
        var evil=evils[i];
        if(evil.health>health){
            var best=evil; //null 
            var heal=evil.health;   
            i++; 
        }      
    }    
    

    var buds=hero.findFriends();
    for(var i=0;i<buds.length;i++){
        var bud=buds[i];  
        
        if(best){
        hero.command(bud, "attack", best);  
        }  
    }
    
   

}

I think I see a misspelled word in your while loop. Also, make sure your i++ is outside of the if statement. Right now, you will only add to i if the evil.health > health which will create an infinite loop. A simple adjustment to the closing curly brace of the if statement will solve this.

var heal=evil.health; //I think you meant it to say health = evil.health