Hey so I solved the level but am trying to fully comprehend the code.
while(true) {
var farthest = null;
var maxDistance = 0;
var enemyIndex = 0;
var enemies = hero.findEnemies();
// Look at all the enemies to figure out which one is farthest away.
while (enemyIndex < enemies.length) {
var target = enemies[enemyIndex];
enemyIndex += 1;
// Is this enemy farther than the farthest we've seen so far?
var distance = hero.distanceTo(target);
if (distance > maxDistance) {
maxDistance = distance;
farthest = target;
}
}
so each time enemyIndex will increase by 1. if my distance to the target is greater than 0 then 0 is my distance to the target.? I tried taking out maxDistance=distance and it worked the same (I figured repeating 0 seemed redundant?)
I tried taking out enemyIndex +=1 but it didn’t work, if the code loop returns true, shouldn’t it move on to the next piece? Like my looking at this how does it know to keep moving further until there are no more targets? When you move closer to another target doesn’t that bring the remaining targets further from your location resulting in an infinite loop? or is that why (enemyIndex < enemies.length) is there so that it stops when it hits the last enemy? (I think I just answered my own question) so is it necessary to put maxDistance = distance?