Level - safety blanket; help with using arrays to attack every other enemy


#1

I want to attack every other enemy until their health is below 0, then switch to the next, next, enemy. To my mind, the below code should work, yet the character just stands there. Please explain to me what I’m doing wrong.

// This is an array of nearby names!
// Enemies are at the 0, 2, 4, and 6 indexes of the array.
var names = [
    "Thabt", "Victor", // 0, 1
    "Leerer", "Alianor", // 2, 3
    "Gorylo", "Millicent", // 4, 5
    "Weeb", "Brom" // 6, 7
];
var ni = 0;
while (ni < names.length) {
    var enemy = names[ni];
    if (enemy.health > 0) {
        hero.attack(enemy);
    }
    else {
        ni +=2;
    }
}

#2

You try to get property of string

for example

if ( "Thabt".health > 0 )

#3

Ni… did you make a mistake? Or did you not mean to put that?


#4

ni = namesIndex

ni starts at 0 so names[ni] = the first name in the names array results. Should I not use ni?


#5

You try to get property of string

for example

if ( “Thabt”.health > 0 )

A string like the above would work but would make my code needlessly long. How can I attack the enemy while their health is above zero WITHOUT specifying an enemies’ name?


#6

I’m wondering if there is a bug with this level because the code

    if (enemy.health > 0) {
        hero.attack(enemy);

Doesn’t seem to work in this level, but it does in other levels (the level wandering souls, for example)


#7

It is interesting
Can you provide your code for the wandering souls
Because base code don’t use strings
It use array of enemies


#8

I think you should maybe define ni…a little bit better. Or make sure ni is a method or function


#9

I think you should increment the ni variable by 1 and not two because then you are skipping over one enemy.


#10

Bugs are hard to be sure of but if you think it’s a bug just post the problem as a bug