# {SOLVED}Long'Range Division - the second one

**URL:** <https://discourse.codecombat.com/t/solved-longrange-division-the-second-one/14788>\
**Category:** Uncategorized\
**Created:** [May 23, 2018, 2:27pm UTC](https://discourse.codecombat.com/t/solved-longrange-division-the-second-one/14788 "2018-05-23T14:27:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JFM](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jfm/32/8173_2.png) [@JFM](https://discourse.codecombat.com/u/JFM)\
**Post date:** [May 23, 2018, 2:27pm UTC](https://discourse.codecombat.com/t/solved-longrange-division-the-second-one/14788/1 "2018-05-23T14:27:47Z")

</div>

I really don’t understand what is wrong with this code… My hero doesn’t detect the second enemy to kill him. Is this the code? Or something is special in the second enemy abilities? Help me…

// 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);  
}  
}

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [May 23, 2018, 4:57pm UTC](https://discourse.codecombat.com/t/solved-longrange-division-the-second-one/14788/2 "2018-05-23T16:57:32Z")

</div>

It’s because you’ve only defined enemy once: `var enemy = hero.findNearestEnemy();` outside the `while(true) {` loop, so inside the `while(true) {` loop it’s only attacking the one you defined once, therefore you’ve got to define enemy _ **inside** _ the `while(true) {` loop. That’s all.  
Hope this helps! 😁

---

<div class="post-metadata">

**Author:** ![JFM](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jfm/32/8173_2.png) [@JFM](https://discourse.codecombat.com/u/JFM)\
**Post date:** [May 23, 2018, 6:50pm UTC](https://discourse.codecombat.com/t/solved-longrange-division-the-second-one/14788/3 "2018-05-23T18:50:10Z")

</div>

Thx Deadpool. I thought about it, but, don’t know why, i didn’t test it… 😑 How stupid ! Thx.

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [May 23, 2018, 6:59pm UTC](https://discourse.codecombat.com/t/solved-longrange-division-the-second-one/14788/4 "2018-05-23T18:59:04Z")

</div>

My pleasure, please could you also put a [SOLVED] in the title description to stop anyone thinking you still had a problem. Also in the future could you use the pre-formatted code button to format your code properly, It’s next to the quote button on the taskbar and it will come up with the text: “type or paste code here” paste your code directly from the game, It would look like this:

```python
// 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);
    }
}

```

Thanks.
