Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Long-Range Division Bug?

I can’t pass the level as the second ogre never appears so I run out of time without completing the quest.

My code is the below:

// Destroy the mines!
// Use `say` to call out the range to the mines.
// Use division to calculate the range.
var enemy = hero.findNearestEnemy();
var distanceToEnemy = hero.distanceTo(enemy);
// Say first Range: distanceToEnemy divided by 3
hero.say(distanceToEnemy / 3);
hero.say("Fire!");
// Say second range: distanceToEnemy divided by 1.5
hero.say(distanceToEnemy / 1.5);
hero.say("Fire!");
// Say these things for motivation. Really. Trust us.
hero.say("Woo hoo!");
hero.say("Here we go!");
hero.say("Charge!!");
// Now, use a while-true loop to attack the enemies.
while (true) {
    if (enemy) {
        hero.attack(enemy);
    }
}

Howdy and welcome to the forum!

In your while loop…what enemy are you testing for and attacking? There are two of them after all :wink:

There is only one enemy labelled as “enemy”, I don’t know how could I attack the other one next to their hostage.

How could I referred to both of them if not with just hero.attack(enemy)?

Define enemy inside the while loop. Each time the loop runs, it will look for an enemy, so once the 1st one is dead, it will find the second.

It worked!

Thanks.

Why should I define enemy again inside the while loop once it’s already globally defined?

1 Like

Because when you defined it as a global, it is static…it will never change. This is actually beneficial for taking out the mines. However, to take out the enemies, you need to re-define it in the loop, update/refreshing it so that it is current, hence ‘tagging’ the second bad guy, once the first is down.

Ok, understood. Thanks again