Hit and Freeze Javascript code problem


#1

Hello everyone.

I’m currently stuck on Hit and Freeze. This is my code. Can you tell me where I went wrong?
edit: copied the wrong code:

// You are trapped. Don't move, it'll be painful.

// This function checks if the enemy is in your attack range.
function inAttackRange(enemy) {
    var distance = hero.distanceTo(enemy);
    // Almost all swords have attack range of 3.
    if (distance <= 3) {
        return true;
    } else {
        return false;
    }
}

// Attack ogres only when they're within reach.
while (true) {
    // Find the nearest enemy and store it in a variable.
    var enemy = hero.findNearestEnemy();
    // Call inAttackRange(enemy), with the enemy as the argument
   var canAttack = inAttackRange(enemy);
    // and save the result in the variable canAttack.
    if (canAttack === true);
    hero.attack;
    // If the result stored in canAttack is true, then attack!
    
}


#2

Two problems:

You don’t want that semi-colon in your if statement, it is ending your if statement before the attack can happen. Also:

Who are you attacking? :wink:


#3

That fixed it! You, sir/madam, are awesome


#4

I think it is a sir.