Lesson 4, how does a function work?


#1

var functionfoodDemand = function("food") {

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

foodDemand ("macaroni");
};

ive been starring at this for ages now and cant figure out what im missing.
It keeps telling me i have a syntax error: unexpected string.
any help?


#2

Your parameter (food) should not be a string. Take out those quotation marks. Also,

Take out the word "function" in your variable.


#3

// Write your foodDemand function below.
// Last hint: In your reusable block of code, end each line
// with a semicolovar foodDemand = function("chocolate") //took away the semicolon(:wink:
var foodDemand = function(food){
console.log("I want to eat" + " " + food);
};
foodDemand("pie");


#4

In addition to what @ragezapper found you need to put the function call:

foodDemand ("macaroni");

outside of the function otherwise the function is not called and if it is you're trapped in a recursion (function loop).


#5

I don't know i tried the same thing but i didn't work


#6

Could you post your code in a new topic and link it to your post?


#7

Change This:

var functionfoodDemand = function("food") {

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

foodDemand ("macaroni");
};

to this:

var functionfoodDemand = function("food") {

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

};
foodDemand ("macaroni");

#8

Ok a few things are wrong with this one.

First this thread was dead for about 2 Month. 
Second it already has answers. 
Third your code has flaws that are already mentioned in these answers.
Fourth what is the difference between your two snippets?

Ok I've found it that you moved the function call from the inside to the outside of the function and that is correct. But please if you post solutions give some more details so that your answer can be helpful to others.

And about those flaws:

No "" around the parameter name food. It should be a variable.
No function in the name of the function functionfoodDemand -> foodDemand
(not quite necessary in JS but demanded by the instructions.)
Matching function names functionfoodDemand != foodDemand

Ok I guess that's all. Hope this helps you to improve further answer. Thank you!