4/13 foodDemand Error


#1

What Am I Doing Wrong?

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

console.log(foodDemand);

I Tried To Switch Food With Pizza But It Still Didn't Work...


#2

The idea of paramteres is that you use a local variable in the function but get the value from outside of the function. So here it is ok to use a variable:

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

but here:

foodDemand(food);

food needs to be a value because that is the value of food inside of your function. If you insert e.g. "pizza" here than as said food will be "pizza" and therefore you'll see "I want to eat pizza". Also as said food is a local variable so it is unknown outside of the function so you probably get a reference error here.


#3

I Know This Already But When I Put It In It Docent Work.


#4
console.log(foodDemand);

as foodDemand is a function this should just give you [Function]. To see a value you would need to call foodDemand by e.g. foodDemand("myFavoriteFood");. Also foodDemand doesn't return something so it doesn't make sense to wrap it in a console.log as you only see undefined if a function is not returning anything.


#5

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".**


#6

++++ function with 1 parameter using return-statement

var myFunction = function( param1 ) {
       //Begin of FUNCTION-BODY
       //myFunction =function= has 1 PARAMETER param1
       //this param1 PARAMETER is used as a -local- VARIABLE
       //throughout the FUNCTION-BODY

      return param1;

      //End of FUNCTION-BODY
      };

you have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 );

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".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the console.log()-method to do a display.

var theResult = myFunction( 4 );
console.log( theResult );

OR directly

console.log( myFunction( 4 ) );

#7

With

var foodDemand = function (food) {
    //Begin of FUNCTION-BODY
        console.log("I want to eat" + " " + food);
    //End of FUNCTION-BODY
};

you have created a foodDemand function which takes 1 parameter food
This food parameter is used as a local Variable throughout the FUNCTION-BODY

Now if you want to call / execute the foodDemand function
you will have to add a pair of parentheses ( ) like
foodDemand();
But as the foodDemand function was defined as having 1 parameter food
you will have to provide 1 argument in your case a string Value "Pizza"
foodDemand("Pizza");
and you will get a Display I want to eat Pizza
( so NO need to encapsulate the function-call into an extra console.log() )

It would have been different if the foodDemand function was defined as

var foodDemand = function (food) {
            return "I want to eat" + " " + food;
};

then you would have to either
capture the returned string Value into a variable and then do a Display like

var theResult = foodDemand("Pizza");
console.log( theResult );

OR directly

console.log( foodDemand("Pizza") );

#8

I have a question,I wrote this code: //
Write your foodDemand function below.
// Last hint: In your reusable block of code, end each line
// with a semicolon (:wink:
var foodDemand = function(food){

console.log("I want to eat" + " " + food);

}
console.log( foodDemand("Pizza"));

And as a result, I have:

I want to eat Pizza
undefined

Why undefined?

Thank you for your help!

Ana


#9

Because here:

console.log( foodDemand("Pizza"));

you print the value of the function call. Which is the value that you return and as you don't return something it is the default undefined.


#10

Thank you very much!! smile: