# Computer Science 5 Level 19 will not work properly. (JavaScript)

It does seem like a bug, and I’m not sure how to fix this or work around it.
So, I have the DEFAULT code that I’m asked to write.

``````function findNearestPair(units) {
var minDistance = 9001;
var nearestPair = ["Nobody", "Nobody"];
// You need to check and compare all pairs of units.
// Iterate all units with indexes "i" from 0 to "units.length".
for(var i = 0; i < units.length; i++){
var unitA = units[i];
// Iterate all units again with indexes "j".
for(var j = 0; j < units.length; i++){
// If "i" is equal to "j", then skip (continue).
if(i == j){
continue;
}
var unitB = units[j];
// Find the distance between the i-th and j-th units.
var distance = unitA.distanceTo(unitB);
// If the distance less than 'minDistance':
if(distance < minDistance){
minDistance = distance
nearestPair = [unitA.id, unitB.id]
}
}
return nearestPair;
}
}

while (true) {
var soldiers = hero.findByType("soldier");
// We know when the cannon shoots.
if (hero.time % 8 === 5) {
// Find which pair of soldiers in danger and protect them.
var pairOfNames = findNearestPair(soldiers);
// Say the soldier's names and wizards will protect them.
hero.say(pairOfNames[0] + " " + pairOfNames[1]);
}
}

``````

The issue is that this, in the function, no matter what I try, no matter what I comment out, ALWAYS returns the error of “Statement Execution limit reached”. It’s impossible for me to find a workaround, and it’s ONLY this level.
It is categorized as a bug because none of the other categories seem to properly fit with this error.

Again, this is the DEFAULT CODE that you are told to write. Hints don’t help either.

If I try to declare both unit variables after

``````if(i == j){
continue;
}
``````

then the first unit (i) is null.

need to change the i to a j or it will repeat forever and j won’t change

oh, didn’t notice lol, thanks for pointing it out!

1 Like