Serven Sum. Where is a mistake?

Hello everyone!
Give me please a hint, what is wrong in this code?
Where I made mistake?

var whiteX = { x: 27, y: 42};
var redX = {x: 151,y: 118};

var traps = hero.findByType("fire-trap");
var trapIndex = 0;
var minTrap = 9999;
var maxTrap = 0;
var enemiesArray = hero.findEnemies();

hero.moveXY(27, 42);
function sumMinAndMax(traps) {
    while (trapIndex < traps.length) {
        if (traps[trapIndex].value < minTrap) {
            minTrap = traps[trapIndex].value;
        }
        if (traps[trapIndex].value > maxTrap) {
            maxTrap = traps[trapIndex].value;
        }
        trapIndex += 1;
    }
    return maxTrap + minTrap;
}

function KillHim(enemiesArray) {
    var enemyIndex = 0;
    var enemy = null;
    while (enemyIndex < enemiesArray.length) {
            enemy = enemiesArray[enemyIndex];
            if(enemy.health > 0 ) {
                hero.attack(enemy);
            }
        enemyIndex += 1;
    }
}

sumMinAndMax(traps);
var say = sumMinAndMax(traps);
hero.say(say);
KillHim(enemiesArray);

Hi, your code is almost right, the problem is in how you use the KillHim function.
You’ve written it as a single instruction which means that it will only be run once. Also, your using the enemiesArray variable, which you only defined once at the top of your code. At that time (as in, at the start of the level), how many enemies were there?

The answer of course is zero, and so when you run the KillHim function, there are no enemies in the array to loop through and attack. And because you only call the KillHim function once, and not inside a loop, then it will only run once, and do nothing, and that’s the end of the execution of any code.

The answer is to repeat using the function, and keep your enemiesArray updated constantly, so that when enemies appear out of nowhere, you will register them, and attack them with your function.

What bit of code could you use to repeat those two actions constantly?

Welcome back Deadpool


image
198
!