4 Introduction to Functions- How does a function work?


Here is the code I have written:

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

The error I'm receiving is SyntaxError: Unexpected string

Could someone please help me fix the code and explain what I've done incorrectly?


var foodDemand =function(food)


@jaheenan77 you should declare your function with the parameter without quotes so it looks like this:

var foodDemand = function(food)

then, in your code block (the code reusable code between the curly brackets), you want to remove the quotes around "food" so that this is identifying the parameter only, like this:

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

try that and see if it works... it should


This is my code

why doesn't it work? Thanks.

 var foodDemand = function(food) {    <---- no semi colon before the bracket!
        console.log("I want to eat" + " " + food);     need the +'s 

That should work.


You might be right, but it doesn't work yet ( "missing ) after argument list")


oh and also put rice in quotation marks "Rice"


Still the same, any more suggestions?


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

This works for me if not for you, refresh the browser.


Thanks! You helped me a lot!


I tried making those changes to my code leaving it as this:

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

But I'm still receiving the "SyntaxError: Unexpected string" error.

Thank you for your explanation of the components of the code, it was helpful


Update to everyone I was able to solve the problem by returning to exercise 3 and comparing my code structure against that code

(Exercise three)
This code was working

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

(Exercise four)
This code was not

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

After removing the quotation marks from food shown here:

var foodDemand=function ("food")

var foodDemand=function (food)

I submitted the code again and it worked as intended


What is wrong?????


food is the parameter of the function and therefore it only exists inside of the function. So instead of food("Pizza") better call your foodDemand function:foodDemand("Pizza");


this should work

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

How does a function work?