FoodDemand


#1

Hi

What I want to do is ask a question such as what food did you eat? and the person can put whatever name they want, such as what did you eat ben.
I tried below but something is wrong and I can't find the problem. this is what I wrote below

var foodDemand=function (name)
{console.log ("What food did you eat "+""+" name")}

foodDemand (Ben)

Can someone correct me please


#2

Fixed it for ya (with notes):

var foodDemand=function (name) { // declaring a foodDemand function with a parameter of (name)
      console.log("What food did you eat" + " " + name); // this will take a string and add the name passed thru it
};

foodDemand ("Ben"); // this calls our foodDemand function while passing string "Ben" into the function (needs quotes since it's a string, and not a predefined variable)

Here's another example of passing a predefined variable into a function (with some optimized code):

var foodType = "Pizza";

var foodDemand = function(food) { // function takes foodType from below and passes it into a local (temporary) food variable
    console.log("I want to eat " + food); // I got rid of the extra " " by adding a space to the end => ("... to eat(space)" + food");
};

foodDemand(foodType); // calling foodDemand with a predefined variable foodType

#3

it's probably because you forgot to add semicolons.
just to add to mfx88 answer you should've used " "`instead` "" `

""
is a empty string, not a string with a 'space'


#4

Can you explain to me in basic terms what a variable is and what a function is, what do they really mean?

Thanks


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.