Computer Science 2 - Munchkin Swarm - Level 18

It keeps saying “RAN OUT OF TIME”. I have tried multiple times. I am coding with JavaScript. I

This is my code:

``````while(true) {
// Check the distance to the nearest enemy.
var nearestEnemy = hero.findNearestEnemy();
var distance = hero.distanceTo(nearestEnemy);
// If it comes closer than 10 meters, cleave it!
if (distance < 10) {
hero.cleave();
}
// Else, attack the "Chest" by name.
else{
hero.attack("Chest");
hero.attack("Chest");
}
}

``````

i think mabey only attack the chest once

1 Like

Try:

``````while(true) {
// Check the distance to the nearest enemy.
var nearestEnemy = hero.findNearestEnemy();
var distance = hero.distanceTo(nearestEnemy);
// If it comes closer than 10 meters, cleave it!
if (nearestEnemy && distance < 10) {
hero.cleave(nearestEnemy);
}
// Else, attack the "Chest" by name.
else {
hero.attack("Chest");
}
}
``````

Doesn’t Work. Still the same Error

Doesn’t Work. Same Error.

and use this pet:

the duck works like this:
you understand what you need to do in the code.
you explain the duck what you need to do
and you look at the code at the same time.
and then you will see the bug!

will this work?

``````while(true) {
// Check the distance to the nearest enemy.
var nearestEnemy = hero.findNearestEnemy();
var distance = hero.distanceTo(nearestEnemy);
// If it comes closer than 10 meters, cleave it!
if (distance < 6) {
hero.cleave();
}
// Else, attack the "Chest" by name.
else{
hero.attack("Chest");
}
}
``````

@Codingisawesome can you post a screenshot of the level and your gear please?
I’ve tried your code with my weakest gear and Tharin and it seems to work fine for me:

1 Like

Doesn’t Work. I tried with smaller numbers too in the distance.

I will try with Tharin and will get back to you.

Hi. It with works with Tharin! Thank you!

Thank you everyone for helping!

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.