Tatical Strike Can't kill 2nd Enemy

I have tried this multiple ways and no matter what I complete the level everything but fail to kill the second enemy. Can someone explain to me what I am doing wrong?

// Defeat the ogres.
hero.moveUp();
hero.moveRight(3);
hero.moveDown(2);

var enemy = hero.findNearestEnemy();
    hero.attack(enemy);
    hero.attack(enemy);
hero.moveLeft();
hero.moveDown();

//EDIT: I solved it but I am unsure as to why this worked opposed to the could I previously stated. Does the variable I made only work for the one instance of attack? Then I have to reinitialize with a new variable to attack the same enemy? But if I had written the code myself and given the enemies the same name then the original code would of worked?

// Defeat the ogres.
hero.moveUp();
hero.moveRight(3);
hero.moveDown(2);

var enemy = hero.findNearestEnemy();
    hero.attack(enemy);
var enemy1 = hero.findNearestEnemy();
    hero.attack(enemy1);
hero.moveLeft();
hero.moveDown();

When you use the attack method twice you are refering to the same Enemy. You need to re update the value of enemy. Use a loop or call the assignment again to give enemy a new value.

You dont need to make enemy and enemy1. You can re use the variable enemy.

How would I do it with a while loop?

// Defeat the ogres.
hero.moveUp();
hero.moveRight(3);
hero.moveDown(2);
while (true) {
var enemy = hero.findNearestEnemy();
    hero.attack(enemy);
}
hero.moveLeft();
hero.moveDown();

With this way he doesn’t break the loop after the enemies are dead. Instead I walk into the wall looking for more enemies.

I take that back dont use a while loop on this map. That loop will only work if you manage to kill the first enemy before the next iteration of the loop.

Alright well at least I solved it the other way but thank you!

Here ill explain whats going on.

Ill use random names just for explaining.

var enemy = hero.findNearestEnemy(); #Enemy = Bill
    hero.attack(enemy); # Attack Bill
var enemy = hero.findNearestEnemy(); #Enemy = Jill
    hero.attack(enemy); # Attack Jill
var enemy = hero.findNearestEnemy(); #Enemy = Bill
    hero.attack(enemy); #Attack Bill
    hero.attack(enemy); #Attack Bill

Once you have finished Bill you needed to re update the variable Enemy in order to get the next one. That is why your updated code works. Changing the variable name didnt solve it. It was because you up dated the name inside Enemy.
.
.
.
Just keep in mind

var enemy = hero.findNearestEnemy(); #Enemy = Bill
    hero.attack(enemy); # Attack Bill
var enemy = hero.findNearestEnemy(); #Enemy = Jill
    hero.attack(enemy); # Attack Jill

This only works because Bill was dead. If he was still alive this would not work.

I get it! Thanks that makes a lot more sense. I thought the hero.findNearestEnemy(); would just find the next nearest enemy with out having to make a new variable. Also I have to have it written the right way or it wont work right?

For Example: This will work like you stated.


var enemy = hero.findNearestEnemy(); #Enemy = Bill
    hero.attack(enemy); # Attack Bill
var enemy1 = hero.findNearestEnemy(); #Enemy = Jill
    hero.attack(enemy1); # Attack Jill

But this will not?

var enemy = hero.findNearestEnemy(); #Enemy = Bill
var enemy1 = hero.findNearestEnemy(); #Enemy = Jill
    hero.attack(enemy); # Attack Bill
    hero.attack(enemy1); # Attack Jill

or will that work and it doesnt matter where I make the variables just as long as the parameter(I think thats what “enemy” is called) is corresponding correctly with the new hero.attack(); function?

var enemy = hero.findNearestEnemy(); #Enemy = Bill
var enemy1 = hero.findNearestEnemy(); #Enemy = Jill
    hero.attack(enemy); # Attack Bill
    hero.attack(enemy1); # Attack Jill

This will not work .findNearestEnemy() finds the closest enemy to you. If you dont change your position then you would be referring to the same enemy.

Alright I think I am getting thanks a lot!

EDIT: Actually one last question why can’/t I initialize variables outside of the while loop? Aren’t any variables initialized outside of something in the global scope and thus are allowed to be accessed in anything? Or is the way the game is set up preventing the use of global scope? Only letting you use local scope?

I updated my code. Sorry I made a mistake. I only updated the variables.

Ignore the (.'s) Thats only for indentation.

You can initalize variables outside of a while loop. Global scope refers to anything on the outter most bracket.

To keep it simple while global can be very complex.

var Enemy = "Bill"

while (True)
{
#This while loop can use Enemy.
}

Ok so if I understand correctly. All I needed to do was reset the variable not change the name of the variable?

So for example

var enemy = hero.findNearestEnemy(); #Enemy = Bill Can be created once? But I can use it over and over and it will just reset?

So then

var enemy = hero.findNearestEnemy(); #Enemy = Bill Set’s the target to Bill
hero.attack(enemy); # Attack Bill Then attacks Bill

Then I reset the variable using the already existing variable right?

var enemy = hero.findNearestEnemy(); #Enemy = Jill This time I reset the variable and it’s focusing the next closest enemy in this case Jill?

hero.attack(enemy); # Attack Jill And now since the variable is set to attack Jill because she is the closest. I now attack and kill her right?

Yes this only works if you managed to kill Bill. If Bill is still alive this wont work only because Bill would still be considered the closest enemy to you.

Note you dont need to use Var again since Enemy was an already created variable. In this case you can just say enemy = hero.findNearestEnemy(); #Enemy = Jill

Right as long as he is dead in this level’s case it only takes one hit but if It took 2 hits and I didn’t use

hero.attack(enemy);
hero.attack(enemy);

twice then I would just stop attacking and get killed right?

Yes. So if it took 2 hits to kill an enemy you would need to find a way to call the attack method 4 times. If you only called the attack method twice then they would kill you.

So then this would work if I needed to attack an enemy twice to kill it, then move onto the next enemy and do the same?

var enemy = hero.findNearestEnemy(); #Enemy = Bill
hero.attack(enemy); #Attack Bill
hero.attack(enemy); #Attack Bill

enemy = hero.findNearestEnemy(): #Enemy = Jill
hero.attack(enemy); #Attack Jill
hero.attack(enemy); #Attack Jill

Yes that would work.

Sweet! Thanks for the help man I really appreciate it! I am on the final challenge of this area now thanks to your explanations :D!

Keep up the good work. :smiley: