# Enemy.type not working?

**URL:** <https://discourse.codecombat.com/t/enemy-type-not-working/1965>\
**Category:** Uncategorized\
**Created:** [December 18, 2014, 6:40pm UTC](https://discourse.codecombat.com/t/enemy-type-not-working/1965 "2014-12-18T18:40:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Querijn](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/querijn/32/1780_2.png) [@Querijn](https://discourse.codecombat.com/u/Querijn)\
**Post date:** [December 18, 2014, 6:40pm UTC](https://discourse.codecombat.com/t/enemy-type-not-working/1965/1 "2014-12-18T18:40:47Z")

</div>

```
var enemy = enemies[curEnemy];
        switch(enemy.type)
        {
        case "munchkin":
            this.attack(enemy);
            this.attack(enemy);
            break;
        case "ogre":
            this.moveXY(41,47);
            break;
        default:
            redo = true;
            curEnemy++;
        }

```

What am I doing wrong here? [http://prntscr.com/5iak90](http://prntscr.com/5iak90)

---

<div class="post-metadata">

**Author:** ![sotonin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sotonin/32/24089_2.png) [@sotonin](https://discourse.codecombat.com/u/sotonin)\
**Post date:** [December 18, 2014, 6:43pm UTC](https://discourse.codecombat.com/t/enemy-type-not-working/1965/2 "2014-12-18T18:43:55Z")

</div>

You need to check if the enemy exists before doing any of that stuff.

```python
var enemy = enemies[curEnemy];
if (enemy) {

```

on a side note, you can accomplish this a bit easier with a regular for loop.

```python
var enemies = this.findEnemies();
if (enemies.length > 0) {
    for(var i = 0; i < enemies.length; ++i) {   
        var enemy = enemies[i];
        if (enemy) {
            // do something here
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Querijn](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/querijn/32/1780_2.png) [@Querijn](https://discourse.codecombat.com/u/Querijn)\
**Post date:** [December 18, 2014, 6:48pm UTC](https://discourse.codecombat.com/t/enemy-type-not-working/1965/3 "2014-12-18T18:48:18Z")

</div>

But I am checking for findEnemies(). Does it always return an array?

---

<div class="post-metadata">

**Author:** ![sotonin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sotonin/32/24089_2.png) [@sotonin](https://discourse.codecombat.com/u/sotonin)\
**Post date:** [December 18, 2014, 6:51pm UTC](https://discourse.codecombat.com/t/enemy-type-not-working/1965/4 "2014-12-18T18:51:30Z")

</div>

Yes. but it may have 1 enemy in it. your code will get to the next index and then the next one could not exist.

Example.

findEnemies has only 1 enemy in it. an array with index[0] only.

using your code. the first round works. but when you increment it to 1 and it enters the loop again. it fails because enemy is undefined and you aren’t checking if (enemy)

---

<div class="post-metadata">

**Author:** ![Querijn](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/querijn/32/1780_2.png) [@Querijn](https://discourse.codecombat.com/u/Querijn)\
**Post date:** [December 18, 2014, 6:51pm UTC](https://discourse.codecombat.com/t/enemy-type-not-working/1965/5 "2014-12-18T18:51:55Z")

</div>

[quote=“sotonin, post:2, topic:1965”]  
var enemies = this.findEnemies();  
if (enemies.length \> 0) {  
for(var i = 0; i \< enemies.length; ++i) {  
var enemy = enemies[i];  
if (enemy) {  
[/quote]You’re right, my bad!
