My function looks pretty textbook, not sure why it's not working


#1

var foodDemand = function (grub) {
console.log("I want to eat" + " " +
"Sushi")
};

foodDemand(grub)


#2

the FUNCTION talk

var myFunc = function( param1, param2) {
       //Begin of  anonymous FUNCTION-BODY
       //VARIABLE -myFunc- has an -anonymous function- assigned
       //this -anonymous function- has 2 PARAMETERS param1 and param2
       //param1 and param2 PARAMETERS are used 
       //as -local- VARIABLES throughout the FUNCTION-BODY

      console.log( param1 + " and " + param2 ) ;

      //End of anonymous FUNCTION-BODY
};

If you want to call/execute the anonymous function
you will have to add a pair of parentheses to the variable myFunc
like
myFunc();
As the anonymous function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren");

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**


#3

As @leonhard_wettengmx_n mentioned, but in simpler text.

var foodDemand = function (grub) {
 console.log("I want to eat" + " " + grub);
};

foodDemand("Sushi");

Because:

var foodDemand = function (grub) {
                             v
                             \--------\
                                      v
 console.log("I want to eat" + " " + grub);
};

So when you pass the function the argument of "Sushi" (foodDemand("Sushi"); ) the code looks at the function like this.

var foodDemand = function ("Sushi") { //THIS IS WHAT THE FUNCTION SEES
 console.log("I want to eat" + " " + "Sushi"); //DO NOT COPY THIS CODE!
};

#4
var foodDemand = function (food){
    console.log("I want to eat" + " " + food);
}

foodDemand("sushi!");

#5

Thank you for a more simplified answer!! @leonhard_wettengmx_n I appreciate your response, but it was a little over my head :smile:

Yes that did work. But on my previous lesson, the code worked when I called foodDemand(grub) which is why I was confused. Unless codeCademy just allowed for me to pass the question initially (which doesn't make for great teaching).

Thanks


#6

With

var foodDemand = function (grub) {
    console.log("I want to eat" + " " + grub);
};

you have created a variable foodDemand
to which you assign a so-called anonymous function
This anonymous function takes 1 parameter grub
This grub parameter is used as a =local= variable throughout the FUNCTION-BODY

As you want to call this anonymous function,
you will have to add a pair of parentheses like
foodDemand();

But as you defined the anonymous function as having 1 parameter
you will have to provide 1 argument
in our case a string Value 'Sushi'

 foodDemand("Sushi");