Operation Killdeer javascript please help

The thing is, I have no idea what code to type in the parentheses of if() :frowning:

I already put the hero.findNearestEnemy; but I didnā€™t include it in the code above by accident.

Okā€¦a function is meant to be a reusable block of code. Sometimes, you can pass a variable to/from the function, as needed. In this case, a variable is not being used, so is not required.

You call the function simply by naming it. In this lesson, the code would start with (look like):

while (true) {
    if (shouldRun()) { //which tests if it is True
        moveXY...
    }
    else {
        kill it while you can
    }
}

I tried doing that but it still does the same thing.

Iā€™ll send my code againā€¦

Re-post your code (all of it this time :wink:)ā€¦Iā€™ll try running it and see what happens.


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

ayyyeā€¦ it still doesnā€™t post the hero.findNearestEnemyā€¦

Anyway, I put the hero.findNearestEnemy above all the code I just posted. Please add that when you run my code :slight_smile: Ahh, now I know how to put the hero.findNearestEnemy. Hereā€™s the code

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

okā€¦I put mine in the else clause (originally done in Python), so letā€™s see what happens with JS.

All right :slight_smile:

YESSS it worked. I couldnā€™t put this as a new post because I canā€™t put more than ā€¦(I forgot)ā€¦posts in a day (Iā€™m a new user). Thank you so much for your help!! :smiley:

LOL I did ā€¦ :grin:

1 Like

Heh, you snuck in your replyā€¦Iā€™m deleting the code I just posted, so it wonā€™t be available to use as a simple copy and paste solution for the next person who gets stuck.