Intro to Functions, Lesson 4 - right and wrong at the same time


Following the instructions for the lesson, I have entered in the following code:

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

Yielding a result of "I want to eat food undefined".

The lesson has stated that my answer was correct, but why did it list 'undefined' and how can I avoid this in the future. Thank you in advance for your assistance


Because there are two console.log() statements... both in the function and when your calling the function.. If you want to console.log() out side the statement just return the value in the function meaning instead of this in your function

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

Do This:

return "I want to eat" + " " + food;

And it should not log out "undefined"! Other than that try to format your code by looking here


Thank you so much! I don't know that I've learned the return command yet, but your help was invaluable.

I've also deduced that maybe two console.logs might be too much and opted to remove it from my function calling and just leave it as


That also removed the undefined. Thanks again!


Yes that was another way to solve it.. Sorry i thought you learned the return command... Its commonly used in Javascript.


Where you haven't learned return yet the way you would've done it would be:
The same as you have got but change this line here



and it would output

I want to eat cake


var orangeCost = function (oranges) {
var val = 5*5;

orangeCost (val);

Don't know what I did wrong here, please don't try this cause this can crash your server.
Almost did on mine.


For this lesson "return" has not been tackled yet. But in your code there are two console logs and the second console log is not needed.

After the first part which is:

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

all you have to do is to "call" the foodDemand function and put your food in the parameter as such:


It should return the right answer.


What lesson number is this?

But I did see that you added a second variable which is not correct:
var val = 5*5;

If you want it to appear in the console, do this instead:

var orangeCost = function (oranges) {
console.log(oranges * 5);

So when you call the function as below it should return "25":


I hope this helps!


Yea it helps thanks for the correction on var val! Sorry for not replying for a while