[SOLVED] Metal Detector https://codecombat.com/play/level/metal-detector?


#1

Hi,

I completed the level…but I feel like my code is wrong or extra. Can anyone take a look and let me know where I went wrong here?

Thank you

// The artillery uses coins as a target.
// You'll be the rangefinder for the artillery.
// Write the function.
function coinDistance() {
    // Find the nearest coin,
    var coin = hero.findNearestItem();
    // If there is a coin, return the distance to it.
    if (coin) {
        var distance = hero.distanceTo(coin);
        return distance;
    }    // Else, return 0 (zero).
    else {
        if (distance === 0) {
            return false;
        }
    }
}
while (true) {
    var distance = coinDistance();
    if (distance > 0) {
        // Say the distance.
        hero.say(distance);
    }
}

#2

I think there is supposed to be either one or two = signs


#3

Two, I think.
(Or at least in Python.)


#4

One “=” is for assignment. It is used for assigning a value, like when defining a variable.

Two “==” is a comparison operator. Some other comparison operators are <=, >=, >, <, . They are for comparing values like

if enemy.health > 0: # is greater than 
    hero.attack(enemy)
if hero.health == hero.maxHealth: # equals
    hero.attack(enemy)