This is impossible: Munchkin Swarm - Javascript

Indeed. It worked like a charm on my computer. No problem what so ever. The munchkin were slaughtered and the chest got open. Maybe it’s a long shot, but I remember having trouble coming back to older levels when I changed my stuff. New glasses or new Watch could mess up the functions you’ re allowed to use. Just in case, here is the stuff I wore when I copy pasted your script. Worked like a charm. I use Chrome on Windows8, just in case.

I didn’t go into too much details, but it seems the munckin waves feel buggy to you. On my computer, the waves are collapsing at 14.5sec and 28.5 sec. Two cleaves, no munkins hitting me in between cleaves. I can’t reproduce your problem.

1 Like

Actually, that is a misperception due to the way JS works. JS does not actually have block level declarations. When you declare with var it always gets declared as the first statement of the scope it belongs to, so it is always available to child scopes. However, it still gets assigned (set a value with =) in the proper order.

loop {
    var enemy = value;   // This is declared before the loop starts, but is only set
                         // when the loop starts.
}

// THIS IS THE SAME AS....

var enemy;

loop {
    enemy = value;
}

So what @Vlevo is saying is, assign it in the loop. Since loop is not a function, it will have the same scope is if it is declared outside of the loop. The difference here is when (and how often) the variable’s value is updated.

var enemy = updateValue();   // This is declared at the very beginning (no matter what)
                             // But it is updated just the once. So it's value will get
                             // stuck at the original value.... forever.
var enemy;
loop {
    enemy = updateValue();   // This is updated everytime the loop runs, so it will
                             // never get stuck on "no enemy" or "dead enemy"
}

In other words, @Vlevo is absolutely correct: where you declare (var) depends on your own code style. Where you assign value (=) does not and has no effect on scope. For it to be useful for this level, it must be set in the loop.

A friend of mine had trouble on this level, too, with code that worked for me. When she pressed submit, to get a new random seed, it worked ok.

It’s on my list of things to look at when I have time to check old levels for improvements… but for now it seems that some random seeds result in unintended behavior. In the meantime… try submitting a few times!

1 Like

while True:
# Check the distance to the nearest enemy.
enemy = hero.findNearestEnemy()

if hero.distanceTo(enemy) > 5: 
    
    hero.attack("Chest")
else:
    hero.cleave(enemy)

this is the code I used it has you attack the chest untill the munchkins can be cleaved