Lurkers javaScript help


#1

I’ve been stuck on the lurker level and my code is killing two of the shaman, but not the third. What am I doing wrong? I’m using javaScript.

var enemies = this.findEnemies();
var enemyIndex = 0;
while (enemyIndex < enemies.length) {
    

// Wrap this section in a while loop to iterate over all enemies.
var enemy = enemies[enemyIndex];
if (enemy.type != 'sand-yak') {
    while (enemy.health > 0) {
        this.attack(enemy);
        enemyIndex = 1 + enemyIndex;
    }
}else{
    enemyIndex = 1 + enemyIndex;
}}

I have tried using different integers in place of enemies.length in the while loop to test if that was the problem, but depending on the integer it doesn’t change anything or exacerbates the situation.
I’m using an if/else in my while loop to increase the enemyIndex regardless of if it is a sand-yak or a shaman.


#2

While I do not see the problem in your code(I suck with arrays) I don’t get the }} at the end of your code
it should be

    }
}

#3

Go through your code. What happens when the enemy you are attacking has more health than you do damage?

enemyIndex is increased more than once. Move enemyIndex outside the if/else just before the closing bracket of the while.


#4

Thanks for your help, I got it by dropping the else!


#5

why I am getting errory by “fix your code”
‘enemy’ was null. use a null check beffore accessing properties. try if enemy:

heres my code is


// findEnemies returns a list of all your enemies.
// Only attack shamans. Don't attack yaks!
var enemies = this.findEnemies();
var enemyIndex = 0;
while (enemyIndex <= enemies.length) {    
// Wrap this section in a while loop to iterate over all enemies.
var enemy = enemies[enemyIndex];
 enemyIndex += 1;
if (enemy.type == 'shaman') {
    while (enemy.health > 0) {
        this.attack(enemy);
    }
}
}

please help me what is the accessing properties. ?


#6

if enemy is null (doesn’t exist) then it can’t have health or position or type or a name or…

if (enemy.type == 'shaman')

you are trying to access the “type” property of an empty “enemy” object.

if (enemy && enemy.type == 'shaman')

or

if (enemy) {
    if (enemy.type == 'shaman')

#7

perfect answer that what I am looking for. and define it . I have seen a lot of access the property of the object. and thank you for explanation!


#8

// Wrap this section in a while loop to iterate over all enemies.
while (enemyIndex < 3) {
enemyIndex = enemyIndex + 1;
var enemy = enemies[enemyIndex];
if (enemy.type == ‘shaman’) {
while (enemy.health > 0) {
this.attack(enemy);
}
}
}

Can someone please tell me why my code doesn’t work. It kills one shaman, then stops.


#9

Hello, kittyheart3, and welcome. Please format your code as per the FAQ.
You have while enemyIndex < 3:. The problem is, there are six enemies, and the comment says specifically to iterate over all enemies. Do that, then come back if you still have problems.


#10

My code only attacks one of the three shamans.

var enemies = this.findEnemies();
var enemyIndex = 0;
// Wrap this section in a while loop to iterate over all enemies.
while (enemyIndex < enemies.length) {
var enemy = enemies[enemyIndex];
if (enemy.type == 'shaman') {
    while (enemy.health > 0) {
        this.attack(enemy);
        enemyIndex += 1;
    }
} enemyIndex += 1;
}

#11

You increment enemyIndex whenever you attack an enemy. You should just increment it when you examine an enemy.


#12

Mine says “Hard execution limit of 3000000 exceeded.”

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

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

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

// Remember to increment enemyIndex

#13

Remember, while is a loop. Therefore, while(enemyIndex < enemies.length); doesn’t work.


#14

Oh, thank you! I forgot the { and the }


#15

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

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

What is wrong with my code? The hero stays in one place.


#16

Please notify if you want to revive dead topics and please format your code according to the FAQ.


#17
if (enemies == 6) {

Try using the length of the enemies array.


#18

There are 4 issues with your code:

  1. if (enemies == 6) {
    This line of code isn’t useful to solve this level. You should delete this whole line (and don’t forget to delete the closing ‘}’ too).

  2. while (enemyIndex > enemies) {
    Did you mean to compare enemyIndex against the length of enemies, as @Hellenar noted?

  3. while (enemyIndex > enemies) {
    The while loop is never entered. Check the logic here.
    Hint: greater than?

  4. ++ enemyIndex;
    If you managed to fix the previous 3 listed issues, then you’ll still have an infinite loop.
    Make sure to include this line inside the correct while loop.

#19

Here’s my code. It still isn’t working. My hero doesn’t move.

while(true) {
    

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.length < enemies.length) {
    

var enemy = enemies[enemyIndex];
if (enemy.type == 'shaman') {
    while (enemy.health > 0) {
        enemyIndex += 1;
        hero.attack(enemy);
    }
}
}
}



#20

Ok, first;y, you don’t need a while-True loop, in fact that makes it not work.
Secondly, you need to increment the enemyIndex regardless of whether it was a sand-yak or a shaman. :grin: