How does a function work output


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-6LzGd/0/4?curriculum_id=506324b3a7dffd00020bf661#


I think the output to the code is wrong. I expect the output to be I want to eat fried food but all I get is I want to eat fried


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

}

foodDemand("fried")


#2

There's no string in your code with the letters f-o-o-d so it seems unlikely that it would print that sequence of characters.


#3

Huh, what do you mean? - the word/string food is in the parenthesis of the console.log - I thought the console.log would add the word food to the end of the output.

The codecademy system says it's correct anyway still but I'm not entirely sure it is.


#4

Your code contains some data that represent characters.
You have three of them. They are called strings. They are:
" ", "I want to eat", "fried"
Nowhere in your code is there data that represents the text f-o-o-d.
If you don't have such data, then you should not expect it to appear in the output. Pehaps you mean for that data to be in the code, but then you would have to add it.

If there's a part in the code that you believe currently contains that data, then you will need to reconsider what that something is.

Note that none of these are getting printed, despite being in your code:

var foodDemand = function (food)
{
console.log (""+""+food);

}

foodDemand("")

That is because none of that is data representing text. They are all instructions to be carried out, not data.


#5

can I ask why the below is in the console.log parenthesis when it's not being used in anyway?

+food);

from here...

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

Why is it even there? - what purpose does it serve? lol


#6

If it's not used in any way, then removing it would have no effect. Is that the case?

(Also, see updated previous response, I have a bad habit of making edits a few minutes after posting)


#7

removing the word food changed it to just I want to eat, but adding the word food made it I want to eat Burrito which is strange


#8

ah I think I've got it now, the word food in the console.log string is the placeholder of the function and the function is called foodDemand so when I call it my foodDemand value is returned...like it outputs just Burrito...

var foodDemand = function (food)
{
console.log (" "+" "+food);

}

foodDemand("Burrito")


#9

If I have a function called add, and I use it like so:

add(2, 3)

Then what do you expect the result of that to be?
Would it be strange that changing the 3 to a 4 would cause the result to be 1 greater?

If the function went something like this:

function add(a, b) {
    return a + b;
}

Then what are a and b in that function? How do they relate to how I invoked the function?


#10

They would be added together I think when called


#11

it seems to be its just like one big loop


#12

By the way, thanks for the help ionatan :wink:


#13

var foodDemand = function (a){
console.log("i want to eat "+""+"banana"+" "+"mango") ;
}
console.log(foodDemand("banana")) ;


#14

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