Calling a function


#1

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

Not sure what my problem is, but clearly there is one.


#2

You have to write this:

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

if you want to call the var then you have to type in the Name of the var and thats foodDemand. in my case i want to add Pizza to the Phrase "I want to eat ... " - so i type in the + to tell the Computer he should add something and then i tell him what he is supposed to add. so the plus within the quotes tells the PC to add sth and the + cake tells him to add the var foodDemand wich is "Pizza"

btw im from Germany so there are probably some mistakes with spelling and grammatic :smiley: hope i could help you!


#3

@sege99 Your right basically right so this is just some rephrasing of what you already said.

To call a function you take the function name and append () for example to call foodDemand you would use foodDemand(). And the other important thing is that the value you use in the () of the function call is assigned to the variable you use in the () at your function definition so

foodDemand("Pizza")

leads to "Pizza" being assigned to cake if you declared your function by:

var foodDemand = function (cake) {

and last but not least I'd call the parameter food and not cake as "Pizza" is not a cake but a food at least most of the time :smile:


#4

you helped me out with that one, so thank you. However I am on a new greeting and Im still facing the same problem. Here is my greeting hopefully you can help me.

var myGreeting = function(ello)
{
console.log("greetings from st. simons" + "" + name)
}

myGreeting("ello")


#5

one problem is that you do not add a space. Here you have two options the first is to append it to your string:

"greetings from st. simons "

and the other one is to add it like this:

"greetings from st. simons" + " "+

but in your code you just added an empty string "". The other problem is that you still have trouble with your parameter. By myGreeting("ello") you assign the value "ello" to the variable ello so when you want to use "ello" in your output you need to use the variable that contains it and not an undefined variable name. Also for passing the exercise you might need to stick to the names mentioned in the instruction.


#6

ok but when i put in:
var myGreeting = function (joe){
console.log("greetings from st. simons" + " " +name);

};
myGreeting("Joe");

The error msg comes up saying i did not pass the greeting a name, however I thought I did pass it a name, Joe.


#7

Yes as said you pass the name ("joe") to your parameter joe and then for mysterious reasons you use name instead when you print the message. So either use the variable joe there as well or name your parameter name instead (the second one would probably be better).


#8

var myGreeting = function (joe) {
console.log("greetings from st. simons" + " " + name);

};
myGreeting("joe");

is that what you meant? If so I still am getting the same message as before. I am thankful for your help.


#9

No what did you change?

What I mean is that you need to use the parameter e.g.

var myGreeting = function (aName) {
    console.log("greetings from st. simons" + " " + aName);

};
myGreeting("joe");

Now in this example "joe" would be assigned to aName and therefore you can use aName in your function as if it would mean "joe". So you can use it as well in your output message.


#10

now that makes sense, hot damn I feel like an idiot.

However, after changing the code I still got the msg "Did not call the greeting. Pass it a name!

"var myGreeting = function (aName) {
console.log(aName);

};
myGreeting("Joe");


#11

Guess your function needs to be named greeting and the output is probably expected to be more than aName, afaik the instructions should mention the names and values they want to see.


#12

:wink: like i said - german english isnt the best :smiley: