# Operation Killdeer javascript please help

**URL:** <https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706>\
**Category:** Uncategorized\
**Created:** [February 24, 2017, 4:18pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706 "2017-02-24T16:18:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jet531](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jet531](https://discourse.codecombat.com/u/Jet531)\
**Post date:** [February 24, 2017, 4:18pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/1 "2017-02-24T16:18:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [February 24, 2017, 4:23pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/2 "2017-02-24T16:23:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jet531](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jet531](https://discourse.codecombat.com/u/Jet531)\
**Post date:** [February 25, 2017, 1:14pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/3 "2017-02-25T13:14:59Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [February 26, 2017, 2:28pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/4 "2017-02-26T14:28:56Z")

</div>

> [@Jet531](#):
>
> return false;

Was that supposed to be there?

---

<div class="post-metadata">

**Author:** ![Jet531](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jet531](https://discourse.codecombat.com/u/Jet531)\
**Post date:** [February 27, 2017, 3:36pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/5 "2017-02-27T15:36:51Z")

</div>

yes it is supposed to be there

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [February 27, 2017, 3:43pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/6 "2017-02-27T15:43:41Z")

</div>

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

Thanks!

---

<div class="post-metadata">

**Author:** ![Jet531](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jet531](https://discourse.codecombat.com/u/Jet531)\
**Post date:** [February 27, 2017, 3:43pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/7 "2017-02-27T15:43:55Z")

</div>

it was there when you start should i take it out

---

<div class="post-metadata">

**Author:** ![Jet531](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jet531](https://discourse.codecombat.com/u/Jet531)\
**Post date:** [February 27, 2017, 3:44pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/8 "2017-02-27T15:44:56Z")

</div>

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

    
}

```

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [February 27, 2017, 3:46pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/9 "2017-02-27T15:46:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [February 27, 2017, 3:48pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/10 "2017-02-27T15:48:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jet531](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jet531](https://discourse.codecombat.com/u/Jet531)\
**Post date:** [February 27, 2017, 3:54pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/11 "2017-02-27T15:54:07Z")

</div>

```python
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?

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [February 27, 2017, 3:56pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/12 "2017-02-27T15:56:50Z")

</div>

Yes…try to run it

---

<div class="post-metadata">

**Author:** ![Jet531](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jet531](https://discourse.codecombat.com/u/Jet531)\
**Post date:** [February 27, 2017, 4:03pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/13 "2017-02-27T16:03:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [February 27, 2017, 4:20pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/14 "2017-02-27T16:20:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)\
**Post date:** [February 27, 2017, 4:39pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/15 "2017-02-27T16:39:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jaaneek](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jaaneek/32/4989_2.png) [@Jaaneek](https://discourse.codecombat.com/u/Jaaneek)\
**Post date:** [February 28, 2017, 3:28pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/16 "2017-02-28T15:28:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kementari](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kementari/32/12678_2.png) [@Kementari](https://discourse.codecombat.com/u/Kementari)\
**Post date:** [March 30, 2020, 2:50pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/17 "2020-03-30T14:50:07Z")

</div>

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

```python

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

---

<div class="post-metadata">

**Author:** ![Kementari](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kementari/32/12678_2.png) [@Kementari](https://discourse.codecombat.com/u/Kementari)\
**Post date:** [March 30, 2020, 2:55pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/18 "2020-03-30T14:55:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [March 30, 2020, 3:11pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/19 "2020-03-30T15:11:06Z")

</div>

Howdy and welcome to the forum!

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

```python
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.

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [March 30, 2020, 3:15pm UTC](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706/20 "2020-03-30T15:15:07Z")

</div>

also…who is the enemy?

[Next page](https://discourse.codecombat.com/t/operation-killdeer-javascript-please-help/10706.md?page=2)
