Level help- Lurkers

// findEnemies returns a list of all your enemies.
// Only attack shamans. Don’t attack yaks!

var enemies = hero.findEnemies();
var enemyIndex = 0;

// Wrap this section in a while loop to iterate all enemies.
// While the enemyIndex is less than the length of enemies
while (enemyIndex < enemies.Length)
var enemy = enemies[enemyIndex];
if (enemy.type == “shaman”) {
while (enemy.health > 0) {
hero.attack(enemy);
}
}
// Remember to increment enemyIndex
enemyIndex =+ 1;

Game gives me an error message on line 11- "TypeError: Cannot read property ‘type’ of undefined`

Hello @Javapaster and welcome to the forum! :partying_face: could you please format your code by using this button:
image
(Just paste your code in after you click that button)
Thank you!
Grzmot

his code is here

// findEnemies returns a list of all your enemies.
// Only attack shamans. Don’t attack yaks!

var enemies = hero.findEnemies();
var enemyIndex = 0;

// Wrap this section in a while loop to iterate all enemies.
// While the enemyIndex is less than the length of enemies
while (enemyIndex < enemies.Length)
var enemy = enemies[enemyIndex];
if (enemy.type == “shaman”) {
while (enemy.health > 0) {
hero.attack(enemy);
}
}
// Remember to increment enemyIndex
enemyIndex =+ 1;

Why do you need enemy.type, try replacing it with just if enemy:

No, you need to find if it is a shaman or else you will attack sand yaks and die

Oh ok than I didn’t know.

In that case your could trying putting if (enemy && enemy.type == "shaman"):

still doesn’t work, gives the same error message.

Try if enemy != 'yeti'

Can you repost your code in the correct format? Like the link shown below.

Hello @Javapaster Can you send me the link to this level?
Lydia

I’m not skilled in javascript, but I suppose you need to check “triple-combo” if there is an enemy and if his type == “shaman” and if his health > 0 before attacking.
UPD
No I was wrong(
Idk what’s wrong exactly but something with enemy defining went wrong( I found another way to define enemy and now there is no pop-up messages about undefined, but my hero kills 2 of 3 shamans and freezes face to face with the 3rd one and code stops(

I think I know what the problem but I just have to double-check it once he posts the code, which he is not doing right now.

The link is https://codecombat.com/play/level/lurkers?

1 Like

I don’t think you should have a capital L.
Also where is your { on this line:

Also I’m not sure whether this is in the right place:

It’s quite hard to see what’s going on so as everyone else has said please post your code formatted.
Danny

2 Likes

@Deadpool198 is completely right.
The thing is in syntax.
Try to click [hints] button above code editor field. Almost all work is done there in 3 hints)

Good luck for all the way)

3 Likes

Try to write:

if (enemy && enemy.type === "shaman"){
   // what you want to do
}

@Javapaster this is supposed to be

enemyIndex += 1

Lydia

1 Like