Bad if enemy detection


when I use this code

loop {
    var enemies = this.findEnemies();
    var enemy = this.findNearest(enemies);

it says that the electrocute is not wrapped in a if enemy statement


no, it says there is no such enemy . . . which is different.

  1. “if enemies” means “are there any enemies” NOT “does the one I’m targeting exists.”

  2. enemies(enemies.length) never exists. you are saying attack the nth enemy of a 0 through (n-1) array of enemies.

So OK, I guess in a quite real sense it IS saying that the electrocute is not wrapped in an if enemy . . . . and it is Correct.

1+2) you are saying: if there exist enemies, then attack the one AFTER the last one on the list…

Your electrocute should probably be using “enemy” just like “if enemy attack(enemy)” (unless you really mean attack some “random” guy who just happens to be at the end of the list of enemies, in which case it should be (length - 1))

I would say this is one of the weirdest debugging problems ever invented . . . . the student/person (you) knows EXACTLY what the problem is and yet you can’t see that you are completely correct. :smile:
('tis actually, a well known phenomenon and the reason why you shouldn’t proof-read your own papers/etc)


Also, if enemies will always return true, because empty arrays in JavaScript are truthy. (In Python they are supposed to be falsy, but they are truthy in CodeCombat because of a limitation in our Python parser.)


Bad parser!

Ah I did not know about the array always returning true

Thank you @nick