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.
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. ?
// 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.
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.
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;
}
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
// 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.
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).
while (enemyIndex > enemies) {
Did you mean to compare enemyIndex against the length of enemies, as @Hellenar noted?
while (enemyIndex > enemies) {
The while loop is never entered. Check the logic here. Hint: greater than?
++ 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.
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);
}
}
}
}
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.