The final Kithmaze, javascript help

Ive completed this level but had a question. If i had the variable outside of the loop or at the top of the loop instead of just before the attack it didn’t work, why is this?? shouldn’t a variable work in this case at the top?? I get the error i can’t see the enemy but if its following the loop that should matter as when i call the variable i can see the enemy?

FAIL…

loop{
   var e =  this.findNearestEnemy();  
 this.moveRight();
 this.moveUp();
 this.moveRight();
 this.attack(e);
 this.attack(e);
 this.moveDown(2);
 this.moveUp();
}

PASS ... 

loop{
    
 this.moveRight();
 this.moveUp();
 this.moveRight();
var e =  this.findNearestEnemy(); 
 this.attack(e);
 this.attack(e);
 this.moveDown(2);
 this.moveUp();
}

When “e” is at the top of the loop, you checked it before an enemy was in range, so “e” was null and the program failed when you tried to attack (null).

The second one works as you get the nearest enemy after you have moved a little and the enemy is now in range.

In later levels, it will (hopefully) teach you to always do a “if (enemy)” check before trying to interact with the enemy - to make sure you have one.

With it been in a variable though why is it been checked shouldn’t this just assign this.findNearestEnemy(); to “e” so it shouldn’t actually execute the line??

Hey Monkeyman,

There’s nothing stored in the variable yet if you to the program to assign a value at the beginning. The hero can’t “see” the enemy and store a value until it is closer, which happens in the middle of the code. Because var = null ( as mikelburk said), it won’t execute the line efficiently.

Thank you for the reply, thats what I’m struggling to understand. i thought a var will store the info and then be called at a later point, not actually check it. So i thought the var is stored first and then their to be used later in the code. I would see it as ‘e’ is not at the top its just been assign. ‘e’ is been called later down the script and thats when ‘e’ is called and the var is excited, when the enemy is in view. So the system shouldn’t check the var at the beginning or thats how i understood variables to work.

Partially true…

The variable will “save” what you store in it the entire time. In this particular case, you are storing the return value of “findNearestEnemy”. At the beginning of your loop, since you don’t have line of sight on the bad guy, “findNearestEnemy” returns null. It is then that null that you are storing.

When you access “e” lower, its stored value is “null”. It doesn’t call findNearestEnemy again, since you didn’t store the function itself, just the return value OF the function the moment you called it.

Now, you can actually do it the other way…

loop{
    var e =  this.findNearestEnemy;  
    this.moveRight();
    this.moveUp();
    this.moveRight();
    this.attack(e());
    this.attack(e());
    this.moveDown(2);
    this.moveUp();
}

This time, rather than storing the return value from “findNearestEnemy”, you are storing a reference to the function itself. Javascript is able to determine this since you didn’t put the parentheses after the function name. Now, when you go to access it, in order to call it as a function, you have to give it the parentheses so it knows to make a function call and not just treat it like a reference.

Thank you for the great explanation, makes total sense and has cleared it up in my head. Thanks again!