Help 5. Functions


#1

Can anyone help me with this function? I don't know whats incorrect.? I have to call the greetings function

This was the badly written function and I have to "fix" it:

// Badly written function with syntax errors!

greeting var func{name}(console.log(name)))}

This was my idea but it doesn't work:

var greeting = function (name) {
console.log(name);
};
greeting = ("Elias")


#2

you allmost got it.

change it to:
greeting("Elias");


#3

@puristaja Great, thank you!!


#5

please help me someone, where is error, it says syntax error

var greeting = function (name); {
console.log("Great to see you," + " " + name);
}

var greeting = function("name");
{
console.log(name);
}
greeting("anjali");


#6

What comes in the () after function here:

var greeting = function("name");

is called parameter and is supposed to be a name of a local variable to the function. So as it is supposed to be a variable there must not be "" around it, because "" would make it become a string (value). The way it gets it's value is by value, called argument, you pass to the function by the function call:

greeting("anjali");

So both things together act like this:

    var greeting = function (name){
        ...
    }
    greeting("anjali"); -> "Great to see you, anjali"
    greeting("arccoder77240"); -> "Great to see you, arccoder77240"

would do the same as:

    var greeting = function (name){
        var name = "anjali";
        ...
    }
    greeting(); -> "Great to see you, anjali"
    greeting(); -> "Great to see you, anjali"

The huge difference is that the value in the second version is static meaning no matter how often you call greeting you'll always get the same result. On the other hand in the first version you pass the value to the function so you can use the exact same function to print whatever name you like just by changing the input value.

Also why do you have 2 versions of the greeting function one is sufficient. And of course no semicolons here:

function (name); {

the {} for functions are mandatory and the semicolon sort of splits these two part that should be together.

Summary:
1 Function
no semicolon in the structure
no "" around the parameter
and take the console message from the first function as it seems to be more interesting than just name :slightly_smiling:


#7

Thank you, this post was very helpful.


#8

Cheers! it was really helfpul!