I am unable to pass computer science 2 level 24

My code works except for one flaw it will not lock to the final enemy. In the level you are tasked with protecting the village and keeping the peasants alive. I am unable to kill the final enemy in time every time I submit It ignores the enemy. Can you please find my error ?

// This function attacks the nearest enemy.
function findAndAttackEnemy() {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
}
}

// Define a function to cleave enemies (but only when the ability is ready).
function findAndCleaveEnemy() {
// Find the nearest enemy:
var enemy = hero.findNearestEnemy();
// If an enemy exists:
if (enemy) {
// And if “cleave” is ready:
hero.isReady;
// It’s time to cleave!
hero.cleave(enemy);
}
}
// In your main loop, patrol, cleave, and attack.
while (true) {
// Move to the patrol point, cleave, and attack.
hero.moveXY(35, 34);
findAndCleaveEnemy();
findAndAttackEnemy();

// Move to the other point:
hero.moveXY(60, 32);
// Use findAndCleaveEnemy function:
hero.findAndCleaveenemy;
// Use findAndAttackEnemy function:
hero.findAndAttackenemy;

}

hello, welcome to the discourse. do you mind putting all of your code inside of the format? it makes it easier to read code. thanks.

1 Like
// This function attacks the nearest enemy.
function findAndAttackEnemy () {
  var enemy = hero.findNearestEnemy();
  if (enemy) {
    hero.attack(enemy);
  }
}

// Define a function to cleave enemies (but only when the ability is ready).
function findAndCleaveEnemy () {
  // Find the nearest enemy:
  var enemy = hero.findNearestEnemy();
  // If an enemy exists:
  if (enemy) {
    // And if “cleave” is ready:
    hero.isReady;
    // It’s time to cleave!
    hero.cleave(enemy);
  }
}
// In your main loop, patrol, cleave, and attack.
while (true) {
  // Move to the patrol point, cleave, and attack.
  hero.moveXY(35, 34);
  findAndCleaveEnemy();
  findAndAttackEnemy();
  // Move to the other point:
  hero.moveXY(60, 32);
  // Use findAndCleaveEnemy function:
  hero.findAndCleaveenemy;
  // Use findAndAttackEnemy function:
  hero.findAndAttackenemy;
}

here, it’s JavaScript, indents don’t matter -_-

I’m going to be the JavaScript console:
hero.isReady is not used anywhere, did you mean to call it as a function?
hero.findAndCleaveenemy is not used anywhere, did you mean to call it as a function?
hero.findAndAttackenemy is not used anywhere, did you mean to call it as a function?

I still am unable to pass this level. I tried @moonwatcher348 code but it didn’t work please help.

He just formatted the code you posted, try using fixing the mistakes he told you about:

1 Like

Wait, the e is capital in the hero.findAndxxxxxxEnemy functions… oof

I have all three of those codes plugged in and i’ve double checked spelling but the code still will not perform the given task.

hmm, i see

This should be done differently, you have to implement the hero.isReady in an if-statement, and this is how it should look like: hero.isReady("cleave"); it needs an argument, in this case it’s cleave

Hint:

if (hero.isReady("cleave")) {
    hero.cleave(enemy)
}

As for this, it is not a built-in method for the hero to use. You should be calling it this way:

// Use findAndCleaveEnemy function:
findAndCleaveEnemy;
// Use findAndAttackEnemy function:
findAndAttackEnemy()

1 Like

Thank You for your help

You’re welcome, did it work?

1 Like