Munchkin Swarm Javascript Bug

Hey! So I can’t finish the Munchkin Swarm level because my character walks off the screen whenever I put this.isReady(“cleave”) into the arguments for my if statement. This is my code:

loop {
var enemy = this.findNearestEnemy();
var distance = this.distanceTo(enemy);
if (enemy < 10, this.isReady(“cleave”)) {
this.cleave(enemy);
}
else {
this.attack(“Chest”);
}
}

Thanks for your help!

@Maerus

1.Walking of the screen

The reason he walks off the screen is because the nearest enemy at the beginning of the level is off the screen,so he goes towards the enemy to cleave him.

2.If Condition

Your if condtion is wrong.And enemy<10 dosent make sense, I think you probably meant distance<10

if (distance < 10 && this.isReady("cleave")) {
    this.cleave();
}

Putting the comma operator in your if condition has a totally different meaning to the if condition.If you like I could explain how the comma operator works

UPDATE : Comma Operator

The comma operator evaluates all the expressions and returns the last expression

var i = (1,2,3,4); //The value of i will be 4  because 4 is the last expression
var j = (1,5,3,1); //Similarly the value of j will be 1 s


if(true,false,true,false){
    // This if block wont get executed.Guess why ? 
    // Because the comma operator returns the last expression which is false.
    // So the first 3 "true,false,true" dosent affect the if condition in anyway
}

Hope this made sense.Feel free to ask any question.

ah! Thank you! That makes a ton of sense :smile:
I guess I was confused since it didn’t return any errors
yeah, I would love to know how it works in Javascript!

thanks again!

So what would be the point of ever using commas if it only returns the last expression?

Well no one really uses comma operator.But its just there.

cool, thanks for the help!

Do you succeed solving this level ?
I can’t get the chest item to attack it.
with:

var chest = this.findNearestItem();

chest is always null…
At the same time:

var enemy = this.findNearestEnemy();

is working fine

1 Like

In fact it works with:

this.attack(“Chest”);

but if findNearestItem could work, it should be better in my opinion.

Ah, good point. The chest isn’t a collectable item; it’s actually sort of kind of an enemy, but for this level we wanted enemies to just be ogres. The sample code includes this.attack("Chest") as a hint.

1 Like

Ok. Agree that the chest in the enemy array is too complexe at this step.
Don’t know if it’s possible to remove the useless feature findNearestItem…In fact, it’s maybe not a problem for beginner who will recopy the “Chest” of the previous level, but maybe just for people who are used to use find methods :smile:

You should be getting the glasses two levels after that so that you can use the distanceTo and shortly thereafter use the findNearestItem to start picking up coins. I don’t know if there’s some bug or what that is getting so many advanced users playing the levels in some other order!

Are you sure ? I get the wooden glasses for “Peasant protection”, just before “Munchkin Swarm”. There we need the “distance to” sort.
So with the wooden glasses, we get “findNearestItem”.
And the wooden glasses are also needed to use “Distance to” in this level.

Oh no, sorry, I was thinking of Ogre Encampment. I’ll update the sample code comments to be slightly more clear about attacking “Chest”.