Question about variables [Javascript] [Solved]

Alright now there are a TON of forum posts on variables but I have a question about this one in particular…

// First, loop through all enemies...

var enemies = hero.findEnemies();
var enemyIndex = 0;
// ... but only attack "thrower" type enemies.
while (enemyIndex < enemies.length) {
    var enemy = enemies[enemyIndex];
    if (enemy.type == "thrower" && enemy.health > 0) {
        hero.attack(enemy);
    }
    enemyIndex ++;
}
// Then loop through all the enemies again...
enemies = hero.findEnemies();
enemyIndex = 0;
// ... and defeat everyone who's still standing.
while (enemyIndex < enemies.length) {
    var enemy = enemies[enemyIndex];
    if (enemy.health > 0) {
        hero.attack(enemy);
    }
    enemyIndex ++;
}

Why do I usually have to identify my variables yet in “enemies = hero.findEnemies();
enemyIndex = 0;” on the second part of my code I did not have to specify it is a variable?

This is the default code for the level:

// First, loop through all enemies...

var enemies = hero.findEnemies();
var enemyIndex = 0;
// ... but only attack "thrower" type enemies.

// Then loop through all the enemies again...
enemies = hero.findEnemies();
enemyIndex = 0;
// ... and defeat everyone who's still standing.

Is the reason I do not have to define it in the second part because it was already defined above? Is this an error on the missions end? (I tried taking away variable from the first section and it returned with an error)

I believe the reason for this is that the enemyIndex value will be higher than 0 when it will get to the next loop.

So it was reset to 0.

another way to explain this is this: default code will return a list of enemies. let’s say there is 14 element in the list.

enemyIndex at the end of the loop will be an integer of 14.

on your next loop you research enemies again, there is less enemies than before, let’s say there is 8 enemies now.

if you ask the computer to get the Enemies[EnemyIndex] you are asking to get the 14th element of the list while there is only 8 element in it.

So I believe the error for that is out of range

2 Likes

Yep! I am getting this now lol also because the variable is predefined so there is no need to define it again. Thank you for all your help this morning Gabriel!

You’re posting a lot of topics.

I have allot of questions! lol Sorry though, 2 of them were essentially the same question just worded differently. I was going to delete one but that doesn’t seem to be an option for basic users (at least not that I could find) I was told feel free to post my questions here… sooo… here I am =D

OO and one I found out the answer myself, I don’t really take a break after asking for help, I keep trying to figure out the solution. Once again I can’t delete topics =( (also was early morning 1 or 2 am. I couldn’t sleep so came on here!)

I assure you It’s not through any lack of reading before I make my posts!

you made twice as many topics

I don’t think asking questions is a bad thing? Is that not what a forum is for? If the purpose of a forum was to keep quiet, why have a forum at all? Maybe I’m just more social =D

It’s nice to see this forum so lively. Your questions have been relevant and some are rather profound. :+1:

2 Likes

You have re-declarations of variables. Is this intentional?

// code
    var enemy = enemies[enemyIndex];
// code
    var enemy = enemies[enemyIndex];
// code

JS Variables

Re-Declaring JavaScript Variables:
If you re-declare a JavaScript variable, it will not lose its value.
The variable carName will still have the value “Volvo” after the execution of these statements:
Example:

var carName = "Volvo";
var carName;
1 Like

Oh, no it wasn’t! lol bad habit, this was the first level I’ve encountered where I redefined the value rather than setting it again and that’s what lead to my question here =) Thank you for catching that! Honestly I’ve been trying to better my code by looking how things have been typed differently in different levels,(which lead to my question here) or I’ll sometimes check out the highscores after to see how other people have written it. Most recently I have figured out there doesn’t seem to be a variation between using ’ or "

@MunkeyShynes Thank you =)

off topic : about searching help

“The man who asks a question is a fool for a minute, 
the man who does not ask is a fool for life.”
 Confucius
1 Like