Village Guard-having trouble


#1

// Patrol the village entrances.
// If you find an enemy, attack it.
while(true) {
hero.moveXY(35, 34);
var leftEnemy = hero.findNearestEnemy();
if (leftEnemy)
hero.attack(leftEnemy);
hero.attack(leftEnemy);
hero.moveXY(59, 37);

var rightenemy = hero.findNearestEnemy();
if (rightenemy) 
    hero.attack(rightenemy);
        hero.attack(enemy);

}


// Now move to the right entrance.
hero.moveXY(60, 31);

// Find the rightEnemy.
// Use "if" to attack if there is a rightEnemy.
var enemy = hero.findNearestEnemy();
if (enemy)
hero.attack(enemy);

    I don't know what is wrong with my code but it says that attack's argument should have type object but got null.  Is there always a target to attack? (use if?)

#2

Problem is enemy, you should attack rightenemy 2x

if (rightenemy) 
    hero.attack(rightenemy);
        hero.attack(enemy);

}

#3

You’re right. @bhuyana all you have to do is change the hero.attack(enemy) to a hero.attack(rightenemy) and reduce the indentation.