Can someone explain to me why this code is not working??
Objective is:
Code given to start with is:
Here is my code:
while(true) {
var flag = hero.findFlag(); // Set variable to find flag
var enemy = hero.findNearestEnemy(); // Set variable to find enemy
if (flag) { // If there is an enemy
hero.pickUpFlag(flag); // Pick up the flag.
}
else if (enemy && hero.isReady("cleave")) { //Else if enemy and cleave is ready
enemy = hero.findNearestEnemy();
hero.cleave(enemy); // Cleave the enemy
} else { // Else
enemy = hero.findNearestEnemy();
hero.attack(enemy); // Attack the enemy
}
}
Here is the Error:
I was able to pass the level with this new code I wrote but it was simply because my armor allows me to tank all the damage and just cleave when its up. Pretty much the same code as above with out the else statement
while(true) {
var flag = hero.findFlag();
var enemy = hero.findNearestEnemy();
if (flag) {
// Pick up the flag.
hero.pickUpFlag(flag);
}
else if (enemy && hero.isReady("cleave")) {
enemy = hero.findNearestEnemy();
// Else, attack!
hero.cleave(enemy);
}
}
Here is what my character does when I do pass. I feel like its cheating though since If I didn’t have all the best armor I would be mowed down probably.
Also this code will work as a second option. Basically just replaced attack with shield. But will shielding work and the attack won’t?
while(true) {
var flag = hero.findFlag(); // Set variable to find flag
var enemy = hero.findNearestEnemy(); // Set variable to find enemy
if (flag) { // If there is an enemy
hero.pickUpFlag(flag); // Pick up the flag.
}
else if (enemy && hero.isReady("cleave")) { //Else if enemy and cleave is ready
enemy = hero.findNearestEnemy();
hero.cleave(enemy); // Cleave the enemy
} else { // Else
enemy = hero.findNearestEnemy();
hero.shield(); // Attack the enemy
}
}