In the level mad-maxer! I've got a problem!help!


#1

loop {
var farthest = null;
var maxDistance = 0;
var enemyIndex = 0;
var enemies = this.findEnemies();

what does “var farthest =null” mean?
what’s null !!!
how i can use it !!!

hope you can understand me … not good at english


#2

loop {
var farthest = null;
var maxDistance = 0;
var enemyIndex = 0;
var enemies = this.findEnemies();

// 查看全部敌人,找出最远的那个。
while (enemyIndex < enemies.length) {
    var target = enemies[enemyIndex];
    enemyIndex ++;
    // 是不是有敌人比我们能看到的最远的敌人还要远?
    var distance = this.distanceTo(target);
    if (distance > maxDistance) {
        maxDistance = distance;
        farthest = target;}

if (farthest) {
// 干掉最远的敌人!
// 如果敌人血量大于0就保持攻击。
while (farthest.health>0) {
this.attack(farthest);
}
}
}
}

though i passed this level, but i still don’t understand.


#3

Hello and welcome to the forums! First of all, please paste your code between 3 backticks (`) according to the FAQ so we can clearly ready it.

First of all:

It means that you define (create) the variable with a value of null (“empty”). If you check this variable now (if (farthest)), it will return false as long as the value is null. As soon as you give it another value, the check will return true because the variable is not “empty” anymore.


Otherwise, your code seems to be correct. To help you understand it better, here are some comments in pseudo-code:

loop {    // repeat the below instructions "forever"
/*
 * define variables
 */

while {    // go through the list of enemies
    if {   // if the current enemy is farther than 'maxDistance'
    /*
     * (note: 'maxDistance' is originally zero)
     * then store its distance in 'maxDistance'
     * and make this enemy the 'farthest'
     */
    } // end if
} // end while

/*
 * Note: at the end of the 'while' loop, when you went through all the enemies
 * 'farthest' will contain the name of the enemy that is the farthest from you
 */

if (farthest) {    // if 'farthest' enemy exists (so it's not 'null')
// (note: there will be a 'farthest' enemy as long as there are enemies)
    while (farthest.health > 0) { // as long as it's alive
    // attack it
    } // end while
} // end if

} // end loop

I hope this helps :blush:


#4

wow,not finish reading your reply,but i am eager to thank u!!

you are a wonderful robot and i have to read your reply seriously!

nice of you

wish you happy everyday


#5

Fell free to type in Chinese one of the mods can type/speak Chinese