Help with solution for "long range division"

i am at level “long range division” and here is my code. There are 2 enemies. This kills only one of my enemies. what am i missing in the code? Please help.
Thank you.

``````// Destroy the mines!
// Use say to call out the range to the mines.
// Use division to calculate the range.

var enemy = this.findNearestEnemy();
var distanceToEnemy = this.distanceTo(enemy);
// Say first Range: distanceToEnemy divided by 3
this.say("first Range" + distanceToEnemy/3);
this.say("Fire!");
// Say second range: distanceToEnemy divided by 1.5
this.say("second range" + distanceToEnemy/1.5);
this.say("Fire!");

// Say these things for motivation. Really. Trust us.
this.say("Woo hoo!");
this.say("Here we go!");
this.say("Charge!!");

3
loop {

this.attack(enemy);
}

// Now, use a while-true loop to attack the enemies.
``````

Look at the loop at the bottom (with a 3 above it? You should remove that, too)

You are attacking the ‘enemy’ that was found at the top of your codeblock, but never trying to find the second enemy.

Thank you. will try this out.

Its working now. thank you.

// 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
var distance1 = hero.distanceTo(distanceToEnemy / 3);
hero.say(distance1);
// Say second range: distanceToEnemy divided by 1.5
var distance2 = hero.distanceTo(distanceToEnemy / 1.5);
hero.say(distance2);

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

@Serg stuck on this one. What am I doing wrong?

Please format your code properly. Put your code between two sets of three backticks ( ` ) so it makes it easier to read.

@serg
ok. so I still don’t get it. Outside of correctly formatting, what must I type in order to win the level? You can clearly see that I gave it my best guess and it wasn’t good enough. Please help me out!

Also, I don’t really understand the difference between using quotation marks and just using parenthesis. I really am trying to learn the language but sometimes I feel like this game is trying to pidgeonhole me into one ‘answer’, which is how it works, but I really want to get to know the principles behind it: IE: really learn.

Properly formatting it in the forums helps us see what you might’ve done wrong. Using the triple backticks ( ` ) before and after your code displays the code like this:

``````while(true) {
}
``````

Also, the difference between quotation marks ( `"` ) and parenthesis ( `(` and `)` ) do two different things within your code.

Parenthesis ( `(` and `)` ) have different uses inside of your code. They usually contain a set of information to perform/use. They can be used to group commands like so:

``````var a = (2 + 3) * 4 // This does 2 + 3 first, then multiplies it by 4! Think of PEMDAS from Maths class.
``````

When they are used to call a method, they are called: call operators:

``````hero.moveLeft(); // No arguments
hero.moveXY(23, 55); // 2 arguments!
``````

Finally they can be used to isolate conditional statements:

``````if(true) {
}
while(hero.health > 20) {
}
``````

Strings ( `"` ) are lengths of text that are not used for programming. The words I’m type are all contained within a string in a database somewhere!

``````"I am a string!" // This is a string.
var stringVariable = "This is also a string" // stringVariable now is a string.
hero.say(stringVariable); // Hero says: "This is also a string."
``````

I hope that helps, and if you format your code according to the FAQ, we might be able to help you debug your code.

I faced this situation - the hero would blow up the mines, but would only attack a single enemy instead of the two present.

So I tried using advanced eye glasses to put the enemies in array

That showed the status as ‘Success’, but with an error - so I tried something else, which worked : -

This is what I put and my hero still only attacks one enemy at the end, could someone read and please advise? Thanks in advance.

Use division to calculate the range.

enemy = hero.findNearestEnemy()

distanceToEnemy = hero.distanceTo(enemy)

distanceone = distanceToEnemy / 3
distancetwo = distanceToEnemy / 1.5

Say first Range: distanceToEnemy divided by 3

hero.say(distanceone)

Say second range: distanceToEnemy divided by 1.5

hero.say(distancetwo)

Say these things for motivation. Really. Trust us.

hero.say(“Woo hoo!”)
hero.say(“Here we go!”)
hero.say(“Charge!!”)
hero.say(“Get em”)

Now, use a while-true loop to attack the enemies.

while True:
hero.findNearestEnemy()
if enemy:
hero.attack(enemy)

shoot i just realised copy and pasting has not included my indents. I have put them in but re-reading this just made me realise I should have put

while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack enemy

I couldn’t see how to edit my question so I have shown what I would have changed here. AND NOW IT WORKS…YAAAYYY Thanks Me.

2 Likes