Can't pass village guard

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

Please format your code according to the FAQ. It’s not hard; all you have to do is highlight it and click the </> button.

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

and the same for right. Hope this helps!

1 Like

help me understand this code it won’t work

Please post your code properly formatted as suggested in the FAQ.