Operation Killdeer javascript please help

Help me please i can not figure this out and i only have 1000 gems what should i do???

3 Likes

Please post your code. According to the FAQ.
https://discourse.codecombat.com/faq

3 Likes
// Lure the ogres into a trap. These ogres are careful.
// They will only follow if the hero is injured.

// This function checks the hero's health 
// and returns a Boolean value.
function shouldRun() {
    if (hero.health < hero.maxHealth / 2) {
        return true;
         hero.moveXY(76, 37);
    } else {
        return false;
    } hero.findNearestEnemy();
    hero.attack(enemy);
}

while (true) {
    // Move to the X only if shouldRun() returns true
    
    hero.moveXY(75, 37);
    // Else, attack!
    
}

3 Likes

Was that supposed to be there?

3 Likes

yes it is supposed to be there

2 Likes

Could you remove the comments from the code? It makes it easier for me to read your code.

Thanks!

2 Likes

it was there when you start should i take it out

2 Likes
function shouldRun() {
    if (hero.health < hero.maxHealth / 2) {
        return true;
         hero.moveXY(76, 37);
    } else {
        return false;
    } hero.findNearestEnemy();
    hero.attack(enemy);
}

while (true) {

    
    hero.moveXY(75, 37);

    
}

3 Likes

Make sure that you have everything lined up in the game. That may be the issue.

3 Likes

On line 6, I think you need if enemy; after hero.findNearestEnemy

3 Likes
function shouldRun() {
    if (hero.health < hero.maxHealth / 2) {
        return true;
        hero.moveXY(76, 37);
    } else {
        return false;
    } hero.findNearestEnemy();
    if (enemy) {
     hero.attack(enemy);   
    }
    
}

while (true) {

    
    hero.moveXY(75, 37);

    
}

like this?

2 Likes

Yes…try to run it

3 Likes

it says that i cant have hero.moveXY after return true

2 Likes

It may be a bug. Check with @Bryukh, I’m not sure about it.

3 Likes

It’s not a bug. Any code after return won’t be called.

4 Likes

Look

if (hero.health < hero.maxHealth / 2) {
return true;
hero.moveXY(76, 37); }

Any command after return wont be called,

And you already have this code here.

while (true) {
hero.moveXY(75, 37);
}

Btw the code above is also wrong but i dont want to do everything for you. For now your main issue its useless hero.moveXY after return.

When you will remove that, you will have to use the function in the while loop

If i wasnt clear enought feel free to ask i will try to answer as best as i can.

3 Likes

I’ve been stuck on this level for a long time. Please tell me what is wrong with my code.


function shouldRun() {
    if (hero.health < hero.maxHealth / 2) {
        return true;
    } else {
        return false;
    }
}

while (true) {
    if (hero.health < hero.maxHealth / 2) {
        hero.moveXY(76, 37);
    
    }
    else {
        hero.attack(enemy);
    }
    }

This code just makes the hero attack the ogres until it dies.

Howdy and welcome to the forum!

You need to be using your function, rather than just coding it again. Something like:

function shouldRun() {
    if (hero.health < hero.maxHealth / 2) {
        return true;
    } else {
        return false;
    }
}

while (true) {
    if the <function> is true
        do this
    if the <function> is not true, then do this
        do that    

I would recommend throwing in a cleave, or other strong attack, if you have the weapon for it.

also…who is the enemy?