Hit and freeze javascript help


#1

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

// and save the result in the variable canAttack.
var canAttack = inAttackRange(enemy);
// If the result stored in canAttack is true, then attack!
if (true) {
    hero.attack(enemy);
    }

}
what am i doing wrong i keep failing. after i kill 5 i die


#2

You need to check if (canAttack == true) { - or at least that’s what I think the format is…I wouldn’t know since I only know Python :sweat_smile:


#3

Nailed it :nut_and_bolt:

Another way to write it is like this:

if (canAttack){

In JS, this will check if canAttack is true, not empty, or not null / none