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

[SOLVED] Can't understand function (JS)

hello, Ive encountered in backwood forest level “leave-it-to-cleaver” how to write a function, i am introduced to “target” but what i do not understand is how does the code know target=enemy as I did not define target = hero.findNearestenemy()
once the loop is running how does it know that target is enemy if target is a paramater only in the function?

// This shows how to define a function called cleaveWhenClose
// The function defines a parameter called `target`
function cleaveWhenClose(target) {
    if (hero.distanceTo(target) < 5) {
        // Put your attack code here
        // If cleave is ready, then cleave target
        if (hero.isReady("cleave")) {
            hero.cleave(target);
        }    // else, just attack `target`!
        else {
            hero.attack(target);
        }
    }
}
// This code is not part of the function.
while (true) {
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        // Note that inside cleaveWhenClose, **we refer to the `enemy` as `target`**. (why? it was not defined)
        cleaveWhenClose(enemy);
    }
}

ty

Hi @Ksensei, welcome to the codecombat discourse!

The reason the function is able to use the variable target is because you put it in the brackets of the function when you “call” the function:

And you define enemy here:

And the function only runs when you call it. It doesn’t run by itself, so every time you run it it uses the thing that you put in the bracket of the function when you call it, in this case: enemy. The thing you put in the bracket of the function is called a parameter.
I hope this helps,
Danny

i think i understood a bit better, am i correct if:
i could use the function twice in the loop and i change what is inside the () according to my needs, for example if there was different type of enemy i would define them both like this (never mind the level, just want to understand the basic idea):

while (true) {
    var enemy = hero.findNearestEnemy();
    var enemy1 = hero.findNearestEnemy();
    if (enemy) {
        cleaveWhenClose(enemy);
    }
    else {
        if (enemy1) {
        cleaveWhenClose(enemy1);
}
}
}

kind of like that?

Yes, that’s exactly right. That’s what’s so good about functions.
E.G.

function findDistanceTo(unit) {
    var distance = hero.distanceTo(unit);
    return distance
}
while (true) {
    var friend = hero.findNearest(hero.findFriends());
    var enemy = hero.findNearestEnemy();
    var distanceToFriend = findDistanceTo(friend);
    var distanceToEnemy = findDistanceTo(enemy);
}

Danny
P.S. you can put “javascript” (no quotes) right after the three ``` at the start of your block of code to make it recognize the quotes and javascript specific key words (e.g. var)

1 Like

Hopefully, just to add a little more clarity. The parameter you specify in the function definition is used inside the function’s code. It is meant to represent an object and can be named as anything you wish.

In the code that calls the function, you (typically) define the parameter. The name in this case is again up to you…what ever makes sense to you.

function useTheParameter(parameter) {
    hero.move(parameter.pos); // hero takes a step towards the item
}

while (true) {
    var functionParameter = hero.findNearestItem();
    useTheParameter(functionParameter);

    var functionParameter = "not gonna work";
    useTheParameter(functionParameter); // returns an error, as the function is expecting an object, not a string
}

The point being, you can name the parameter anything that makes sense to you, even if the function def and and the function call have different names. What is critical is that you pass what the function is expecting…in this example, it is not expecting a string as the object.

3 Likes