# Bad if enemy detection

**URL:** <https://discourse.codecombat.com/t/bad-if-enemy-detection/3752>\
**Category:** Uncategorized\
**Created:** [May 3, 2015, 3:28am UTC](https://discourse.codecombat.com/t/bad-if-enemy-detection/3752 "2015-05-03T03:28:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Feinty](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/feinty/32/1869_2.png) [@Feinty](https://discourse.codecombat.com/u/Feinty)\
**Post date:** [May 3, 2015, 3:28am UTC](https://discourse.codecombat.com/t/bad-if-enemy-detection/3752/1 "2015-05-03T03:28:05Z")

</div>

when I use this code

```python
loop {
    var enemies = this.findEnemies();
    var enemy = this.findNearest(enemies);
    if(this.isReady("electrocute")){
        if(enemies){
            this.electrocute(enemies[enemies.length]); 
        }
    }
    if(enemy){
        this.attack(enemy);
    }
}

```

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

---

<div class="post-metadata">

**Author:** ![Vlevo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vlevo/32/2074_2.png) [@Vlevo](https://discourse.codecombat.com/u/Vlevo)\
**Post date:** [May 3, 2015, 4:11am UTC](https://discourse.codecombat.com/t/bad-if-enemy-detection/3752/2 "2015-05-03T04:11:35Z")

</div>

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. 😄  
('tis actually, a well known phenomenon and the reason why you shouldn’t proof-read your own papers/etc)

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nick/32/784_2.png) [@nick](https://discourse.codecombat.com/u/nick)\
**Post date:** [May 3, 2015, 10:54pm UTC](https://discourse.codecombat.com/t/bad-if-enemy-detection/3752/3 "2015-05-03T22:54:08Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![Feinty](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/feinty/32/1869_2.png) [@Feinty](https://discourse.codecombat.com/u/Feinty)\
**Post date:** [May 5, 2015, 3:04am UTC](https://discourse.codecombat.com/t/bad-if-enemy-detection/3752/4 "2015-05-05T03:04:24Z")

</div>

> [@nick](#):
>
> (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
