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

Hit and Freeze support

Please help me!
I’m at Hit and Freeze level, but I can’t do it. It says “Ran out of time.”
My code:

while (true) {

var enemy = hero.findNearestEnemy();

} function inAttackRange(enemy) {

if (canAttack === true) {
    hero.attack(enemy);
}

}

What could be the mistake here?

At a quick glance and if this is all your code, you closed your while loop after the enemy variable so you never run anything else other than finding the enemy.

A few more tips, functions should be built outside of your running code, I always put them at the top. Then you call the function within the code.

function inAttackRange(enemy){
    return 
}
while(true){
    inattackRange(enemy) //example
}

Is canAttack a function or a variable that you are checking? I don’t see that part specified in this code.

Thanks for your reply.
canAtack is a variable in my code.
I can’t do the part of the code where this instruction is:

Call the inAttackRange (enemy) function with the enemy parameter.
and save the value in the canAttack variable.
If the value stored in canAttack is true, attack!

How can I do this?

You’re close, when you call a function you don’t need to write “function” again you just write “inAttackRange(enemy)”. Just like you would use hero.findNearestEnemy() but without the “hero.”.
You need to make a variable called canAttack, this will be a boolean value, meaning it’s either “true”, or “false”. Then use the function inAttackRange(enemy) to give it a value (true or false).
My main point is that you function is the same kind of function as hero.findNearest enemy and that when you “call” it you only need to put the name and the brackets (with enemy inside) in.
I hope this helps.
:lion: :lion: :lion:

I reviewed the level to see how it starts you out.

First part, call the inAttackRange (enemy) function with the enemy parameter and save (assign) the value in the canAttack variable. Combine the 3 parts together all in one line to build the variable with the function value.

  1. create a variable canAttack
  2. to save (assign) the value use the = like with the enemy variable
  3. call the inAttackRange(enemy) function with the enemy variable you used to find your nearest enemy as the parameter.

Second part, If the value stored in canAttack is true,attack! You already had this set up correctly. Since the Function returns the value true or false, the variable will have the value true or false.

if (canAttack === true) {
    hero.attack(enemy);
}

Thank for your reply.
I could do the level with help​:joy::joy::joy:

Thanks for your reply.
I could do the level with help​:joy::joy::joy:

Thanks for your reply