i can’t pass the level village guard i’m using Javascript and my problem is that once i get to the left for the second time the ogre comes but my charecter doesn’t attack it. and it also sometimes says that i need to put an “if” command before a second attack when that is essentially obsolete.
my code as of now looks like this
loop
{
this.moveXY(35, 34);
var leftEnemy = this.findNearestEnemy();
if (leftEnemy)
this.attack(leftEnemy);
this.attack(leftEnemy);
this.moveXY(60, 32);
var rightEnemy = this.findNearestEnemy();
if (rightEnemy)
this.attack(rightEnemy);
this.attack(rightEnemy);
}
If you leave off the {} around an if-statement body, then only the first statement after the if will be part of it, regardless of your indentation/intentions. So you will always run the second attack for left and right, 'cause those aren’t part of the if.
So to fix it, you can do:
if (leftEnemy) {
this.attack(leftEnemy);
this.attack(leftEnemy);
}