[Solved] Magic Exam, please help


Hi guys. I know I have a problem in my code, but can’t locate it. I don’t know why the “while” loop stops executing, because the condition is always “true” and the if else statement that i need, the –else if (heroY === 24)— is also definitely correct.
I only want to know why the “while” loop stops. Thank you in advance.
Here is my code:

hero.moveXY(18, 40);
var heroY;
heroY = hero.pos.y;
while (true) {
    heroY = hero.pos.y;
    hero.say("starting while");
    var enemy = hero.findNearestEnemy();
    var friend = hero.findNearestFriend();
    var item = hero.findNearestItem();
    if (enemy) {
        if (hero.canCast("force-bolt")) {
            hero.cast("force-bolt", enemy);
    } else if (friend) {
        if (hero.canCast("heal")) {
            hero.cast("heal", friend);
    } else if (item) {
        if (item.type == "potion") {
            hero.say("Potion! Yay!");
            hero.moveXY(item.pos.x, item.pos.y);
    } else if (heroY === 40) {
        hero.say("moving down");
        hero.moveXY(hero.pos.x, hero.pos.y - 16);
    } else if (heroY === 24) {
        hero.moveXY(hero.pos.x + 16, hero.pos.y + 16);

Please help, I just began coding. Want to learn.


Enderlord, they are not…The "enemy if…"statement is closed before the “friend if…” statement begins. I wish it was that easy…:roll_eyes:


Ohhhh…Sorry @Kaija_Solod :sob:


I dont know why the while loop stops (It could be an equipment problem) but i do know this.
Your soloution will not work. You will have to check the type (For example)

while (true){
    enemy = hero.findNearestEnemy();
    if (enemy && enemy.type == "munchkin"){

Here is a pseudo code example

function Check(x, y){
    hero.move(x, y);
    enemy = hero.findNearestEnemy();
    if (enemy && enemy.type == "munchkin"){

You would have to keep on checking all the possible options, then you would call the function and give it some parameters so the hero would move to to parameters and check all of the options


Enderlord, my problem is not the type of something. My problem is that my while loop stops and the program ignores item.pos.y the second time, when I instruct the hero to go to the next (3rd, steel) door.


it is because of the random seeds. Sometimes it isn’t a potion. If there isn’t a potion then all of the conditions in your program would be false, so the program would exit out of the while loop. What happens if it is not a potion? Then you have to give the program an else statement (For example you would move to the next door)


Try something simple as:

for ( var x = 18; x <= 66;  x += 16)
    for ( var y = 40; y >= 24;  y -= 16){
         hero.moveXY(x, y); 
        //  heroTakeAction()

Do all checks, attacks etc… in heroTakeAction(). And return in the same spot after that.


It only gives you random seeds when you hit “Submit” button, which I haven’t done yet. I know that there are random seeds, but during any game, the hero’s y-coordinate is always known. Everything else is out of question.


Actually, I don’t think I learned about "for " loop. This is a level without "for " loops I believe. Is there any way to understand the problem of my program not seeing hero’s y-coordinate?


You can do the same thing with a nested while loop:

var x = 18;
while (x <= 66){
    var y = 40;
    while (y >= 24){
        hero.moveXY(x, y); 
        //  heroTakeAction();
        y -= 16;
    x += 16;


Xython, thank you I’ll try it out, but I want to underdstand the problem with particularly MY code. I figured out, if I change the x-coordinate, I have no problem. But with changing y-coordinate, I do.


I think the closest answer to your question is:

var heroX = 18;
var heroY = 40 ;
while (true) {
     hero.moveXY(heroX, heroY);
     if ( Math.abs(hero.pos.y - 40) < 0.1) {
        // hero.say("moving down");
        heroY = hero.pos.y - 16;
    else if (Math.abs(hero.pos.y - 24) < 0.1) {
        // hero.say("moving up");
        heroX = hero.pos.x + 16;
        heroY = hero.pos.y + 16;

See JavaScript | Math.abs( ) function
The hero will never be at the exact point you are sending him/her so you have to suppose some minimal error in the coordinates. .


Remember that you can onley use Math functions if you have programation V


We are all as poor as a church mouse in this level…

You really don’t need heroX & heroY:

hero.moveXY(18, 40);
while (true) {
    // heroTakeAction(); 
    if ( Math.abs(hero.pos.y - 40) < 0.1)
        hero.moveXY(hero.pos.x, hero.pos.y - 16);
    else if (Math.abs(hero.pos.y - 24) < 0.1)
        hero.moveXY(hero.pos.x + 16,hero.pos.y + 16); 

Congratulations Kaija_Solod ! Your idea is better than the way I have finished the level.


Xython, look guys, I know of ways to complete this level using my own knowledge and using the ways from later (more advanced) levels. I don’t care about these ways. I want to understand how code works. If I have a mistake I want to correct it. I clearly have a mistake here with Y-coordinate, that’s it. I only need to understand what’s the problem with this specific Y-coordinate in my code. I thank you all for the replies!:raised_hands:

// your code
while (true) {
// your code
    } else if (Math.abs(heroY - 40) < 0.1 ) { // LINE CORRECTED
 // because it is straight down the original code can remain if you want to ope only 3 doors
// your code
    } else if (Math.abs(heroY - 24)< 0.1) { // LINE CORRECTED
// your code

This is your code with faulty lines corrected , you can try it.
Another edit :slight_smile:
I put console.log('heroY: ’ + heroY); straight after while (true) { to see the real coordinates:


so I changed the code to:

    } else if (heroY.toPrecision(10) == 40) {
        hero.say("moving down");
        hero.moveXY(hero.pos.x, hero.pos.y - 16);
    } else if (heroY.toPrecision(10) == 24) {
        hero.moveXY(hero.pos.x + 16, hero.pos.y + 16);

JavaScript toPrecision() Method
I also started learning java script recently.


there has been NO MATH LEVEL for me yet. Please read my last message.


Hi @Kaija_Solod,

This is not needed, since you’re defining heroY here:

Also the hero.says(), it’s absolutely fine if thinks it helps you understand what’s going on; but I would advise not using them in your final result.
The problem lies where you think it doesn’t, which I think is why you’re having trouble finding it:

Why have you used === here, and == here:

(just saying == is the right way, well the way that works)
I’m not saying that will solve all your problems with this level (if you have any more), but that’s definitely one.
And I think @xython and enderlord were trying to help you actually finish the level with what you’re trying to do, but to me it looks like you’re not done, but you’ve run into an error early. Not sure just wanted to see.
Also can you not class code problems under “adventurer”, I know it’s hard to find a suitable category, but “uncategorized” is what all the senior discourse people say.
Hope this helps,
:lion: :lion: :lion:


Deadpool thanks a ton, the thing is I stated I don’t need help with completing the level, but with understanding why my existing code doesn’t work. That’s why I found almost every answer irrelevant.
Now, as to your answer. I see that declaring variables several times is not needed. And I tried using == in my code instead of ===, but it doens’t make a difference. Btw I don’t see how hero.say() could hurt. In short, here is the edited code which doesn’t work:

hero.moveXY(18, 40);
while (true) {
    hero.say("starting while");
 if (hero.pos.x == 18 && hero.pos.y == 40) {
        hero.say("moving down");
        hero.moveXY(hero.pos.x, hero.pos.y - 16); //this works
        hero.say("At this point I SHOULD have changed my hero.pos.y to 24");
    } else if (hero.pos.y ==  24) {//here it works only with x
        hero.say("moving to the steel door");
    } else if (hero.pos.y != 24) {
        hero.say("My Y-position hasn't changed!");

Here even when the hero clearly has pos.y == 24 it still executed the last IF.


Thanks for the bugs, Admins, and for the whole week of my wasted nerves and time. The y-coordinate was 23.9999999999998 and not 24.