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;
}
}
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?