How does a function work? - 4/13


#1

Hi,
need help with syntax which looks like this -

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

Did i got the answer wrong?


#2

The problem is here:

var foodDemand = function ("food") {

namely the "" around food. The idea of a parameter (the things between the () of a function) is that it is a local variable to the function that get's its value by the function call ( e.g. foodDemand("Pasta");). So what you need to do is to just delete the "" now food is a variable and by running your code you should see that it gets the value "Pasta".


#3

Thank you!
I was wondering on this for good 5 or 10 minutes.

Cheers.


#4

why is it foodDemand("Pasta") and not foodDemand = "Pasta" ?
what is the difference?


#5

This is what I did:
var foodDemand = function(foodDemand) {
console.log("I want to eat" + " " + foodDemand)
};
foodDemand ("candy");


#6

The code is ok, but it's not a good idea to use the function name as the name of a parameter as well. Currenctly we're not using it, but this shadows the name of the function inside of it, meaning that you can no longer foodDemand in foodDemand as a function but just as the value you pass to it.

So I'd recommend to choose a different name as you now have 2 variables foodDemand that share nothing but their name and which might be confusive in the long run.


#7
foodDemand("Pasta")

this calls the function (stop currenct program, jump to the function, assign "Pasta" to the first parameter) and this overwrites the function with "Pasta":

 foodDemand = "Pasta"

So after this foodDemand will be "Pasta" but no longer a function.


#8

i have that:

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

where's my mistake? the console says "It looks like you didn't print anything to the console."


#9

I did this:
var foodDemand = function(foodDemand) {
console.log("I want to eat" + " " + foodDemand)
};
foodDemand ("candy");


#10

Thanks that helped alot