Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

[SOLVED] Borrowed sword. What's wrong?


#1

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);           
                                                 }   
                                        }
                                }
                        }

#2

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


#3

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; }
    }
}

#4

Your function doesn’t return a value…


#5

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++; 
        }
    }
}

#6

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()
    }
   }

#7

Thank you so much!!! Victory)