Level: closing the distance (JS)

Equipped with:
maghohany glasses and simple sword

I have wrote the following code:

this.moveRight();

var enemy = this.findEnemies();
var enemy = this.findNearest(enemy);

this.attack(enemy);
this.attack(enemy);
this.moveRight();
this.moveRight();
this.moveUp();
this.attack(enemy);
this.attack(enemy);
this.moveDown();
this.moveRight();

I cant understand why the character does not find the second enemy, it insists in returning to the dead enemy.

Usually in the other “kill all” levels I only needed this inside a loop:

var enemy = this.findEnemies();
var enemy = this.findNearest(enemy);

this.attack(enemy);

or (using the “glasses”)

this.attack(this.findNearestEnemy());

I can’t understand…

Because you are not finding the Nearest Enemy again between the first two attack commands and the second two attack commands, then the second two attacks are still looking to attack the original enemy you found - you will need to find a new enemy after killing the first one.

When you use this command:

enemy = self.findNearestEnemy()

You are doing a search at that instant for the nearest enemy. You are not saying that enemy will always mean to do a search. So, once the first enemy dies you need to look for a new enemy.

It worked in the loop because everything in a loop is repeated over and over.

Hope that helps!

I used this and it worked just fine

[redacted]

I understand the urge to help others, but posting correct code is not allowed. It destroys the point of the game. Sorry. :frowning: