Level:borrowed sword help pls


#1

Hello, I’m doing exactly what the instructions told me to do for the level but my archers just aren’t strong enough for the job. What do I do?

loop{
var enemies = this.findEnemies();
var counter = 0;
var mostHealth = enemies[counter];
while(counter < enemies.length){
if(enemies[counter].health > mostHealth.health){
mostHealth = enemies[counter];
}
counter++;
}

var friends = this.findFriends();
var i = 0;
while(i < friends.length){
    
    this.command(friends[i],"attack",mostHealth);
i++;
}

}


#3

Please format your code as described in the FAQ. This makes it more likely that others will help you as they don’t have to do the formatting by themselves.


If I see that right I win by using (almost) the same code. I just have a check that mostHealth actually exists.

Have you tried to submit again? Submitting gives you a new random seed, possibly resulting in another outcome.


#4

I met same problem with “Borrowed sword” level. Even if i use solution from video two Yetis remains and they kill all my archers. I tried to complete it 10-15 times.

My solution:

loop:
    friends = hero.findFriends()
    enemies = hero.findEnemies()
    
    bestHealth = 0
    healthiestEnemy = None
    for enemy in enemies:
        if enemy.health > bestHealth:
            healthiestEnemy = enemy
            bestHealth = enemy.health
    
    if healthiestEnemy:
        for friend in friends:
            hero.command(friend, "attack", healthiestEnemy)

#5

I got it with this code (only partially shown to avoid spoiling the level), even though there was an error.

loop{
....
while(counter < enemies.length){
if(enemies[counter].health > mostHealth.health){
mostHealth = enemies[counter];
}
counter++;
}

var friends = this.findFriends();
var i = 0;
while(i < friends.length && enemies){

    this.command(friends[i],"attack",mostHealth);
i++;
}
}



#6
# For this level, your hero doesn't fight.
# Command your archers to focus fire on the enemy with the most health!
while True:
    friend = hero.findFriends()
    enemies = hero.findEnemies()
    highestHp =None 
    bestHealth=0
    for enemy in enemies:
        if enemy.health >= bestHealth:
            highestHp = enemy
            bestHealth = enemy.health
            
    if highestHp:
        friend = hero.findFriends()
        
        hero.command(friend, "attack", highestHp)

If someone would be so kind as to tell me how to tell the archers how to attack the highestHp enemy … i get the error that , that is an array … and i’m really really stuck


#7

It’s because you’re trying to command an array: friend = hero.findFriends() even though you’ve defined friend instead of friends which is a bit confusing. What you need to do is make a for loop and command each individual friend in hero.findFriends()


#8

Sir I just wanted to say thank you yet again :smiley: It worked !


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

Hey.
Can you pls tell me what am I doing wrong here?