Help for Magic Exam!

// Try to get the best grade (gold) at the magic exam.
// Move to each X mark, then use a spell.
while (true) {
    var enemy = hero.findNearestEnemy();
    var friend = hero.findNearestFriend();
    var item = hero.findNearestItem();
    hero.moveXY(18, 40);
    if (enemy) {
        hero.cast("force-bolt", enemy);
    }
    if (friend) {
        hero.cast("heal", friend);
    }
}
hero.moveXY(18, 24);
if (enemy) {
    hero.cast("force-bolt", enemy);
}
if (friend) {
    {
        hero.cast("heal", friend);
    }
    hero.moveXY(34, 40);
    if (enemy) {
        hero.cast("shrink", enemy);
        hero.cast("force-bolt", enemy);
        if (friend) {
            hero.cast("grow", friend);
        }
        hero.moveXY(34, 24);
        if (enemy) {
            hero.cast("shrink", enemy);
            hero.cast("force-bolt", enemy);
            if (friend) {
                hero.cast("grow", friend);
            }
            hero.moveXY(50, 40);
            if (enemy) {
                hero.cast("poison-cloud", enemy);
            }
            if (friend) {
                hero.cast("regen", friend);
                hero.moveXY(50, 24);
                if (enemy) {
                    hero.cast("poison-cloud", enemy);
                }
                if (friend) {
                    hero.cast("regen", friend);
                }
                hero.moveXY(66, 40);
                if (item && item.type == poison) {
                    hero.moveXY(item.pos.x, item.pos.y);
                    hero.cast("regen", hero);
                    if (item && item.type == potion) {
                        hero.moveXY(item.pos.x, item.pos.y);
                        hero.moveXY(66, 24);
                        if (item && item.type == poison) {
                            hero.moveXY(item.pos.x, item.pos.y);
                            hero.cast("regen", hero);
                            if (item && item.type == potion) {
                                hero.moveXY(item.pos.x, item.pos.y);
                            }
                        }
                    }
                }
            }
        }
    }
}

Hello welcome to the forum!

This level allows you to write your own code so it is challenging to see just from your code what may be causing trouble. If you haven’t already, check out the hints for this level in the top right corner if the level doesn’t make sense. Also, please provide a little explanation of the problems you may be having?

  1. Are you receiving an error?
  2. Does your hero just stop at some point or do something you can’t understand why?
  3. Is there a particular part of the level that you don’t know how to write the code to complete?

My hero is stopping at a certain point, even though there is no error warning, and I have completed my code to the end. I also have checked all the hints.

Your code is:

while (true) {
// do something and if a condition is met the last action is
        hero.cast("heal", friend);
}

You never escape from this infinite while loop. Reconsider your logic.

After trying your code, I noticed that you closed out your while (true) loop after your first door. The curly brace placement is critical in javascript and can make it difficult to see where you are at. Not sure if you know this tip, but you can place your cursor on the open curly brace and it will show you the closing one paired up. The snapshot below shows the closed while loop and the tip to find the closing curly brace.

close%20loop

1 Like

Thank You for your help :slight_smile: