Erroring on index value after a false conditional

It’s hard to figure out how to describe this in a one sentence title.

Let’s consider this code:

for (var peonIndex = 0; peonIndex < peons.length; peonIndex++) {
  var peon = peons[peonIndex];
  var item = peon.getNearest(items);
  var distance = peon.distance({x:1,y:1});
}

When peons.length == 0, the getNearest() does not throw an error, but for distance() I get an undefined is not a function.

Even if I wrap the above code in

if ( peons.length > 0 ) { }

I still get the same error. Is there some error detection happening to the code before it starts running? Any ideas on a way around this?

I went to reproduce this one, but couldn’t. Took a look at the code in your session, though, and think you might have a typo on the distance (distanc). It used to be able to tell you an error message like, "Durbo has no method 'distanc'. Did you mean 'distance'?", but it looks like that’s not working. I’ll need to fix it: https://github.com/codecombat/aether/issues/64