How do I call a greeting function?HELP!


#1

var sayHello = function (myname) {
console.log("Great to see you," + " " + "myname");
};
This is my code(I won't post my name)but it keeps saying "You did not call the greeting function. Pass it a name!".
What do I do!


#2

do your code than at the end put console.log(sayHello(your name goes here));


#3

Can you define that more?


#4

sure. your code needs 2 be
var sayHello = function (myname) {
console.log("Great to see you," + " " + "myname");
}
console.log(sayHello(your name))
thats pretty much it.


#5

so when you put
"my name" do i put my name?


#6

yes at the very end of your function and put a semi colen at the end to


#7

I actually did it like this
"greeting("myname")"


#8

yay! :smiley: glad it helped


#9

Could you post your final code pls? Because from what I see till now it seems like you still have some confusion about whether the function should be named greeting or sayHello and whether you want to stringify your parameter or not.


#10

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


#11

Ok as feared :frowning:
Well let's go through it and see if we can bring some light into this.
First of all you need to use a matching names for your function (I guess the exercise would suggest greeting). The reason why this is important is that currently your sayHello function is never executed and you should get an error that greeting is neither defined nor a function. So when you use:

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

you at least have a defined variable greeting that calls the greeting function. Now the next problem is that greeting(x) should output Great to see you, x no matter what x is. In your case it works as you provide "myname" and see "myname" but for any other value you would get "myname" as well. What we can use here is the parameter. In short a parameter is just a regular local variable so:

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

would for the moment do the same job. The special thing about parameters and the reason they have a special place in your function is that they get their values from the function call. e.g. described on your case:

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

here "myname" the so called argument would be assigned to name so inside of the function for this specific function call name could be used as "myname". Which means that you can replace the hard coded "myname" with name:

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

Now as said for this example it doesn't make a difference as it will still output Great to see you, myname but try for example

greeting("jpdav");
greeting("haxor789");
greeting("dr_snickerdoodle");

before and after applying the changes and you should see that parameters can be a really useful thing :smile:
Hope this helps a bit understanding the concept.