Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Amber Tai
596 PointsJavaScript Quiz - 2 function questions.
Hello there,
In the video, Quiz has 2 function questions. I'd like to understand clearly as to how to interpret them when I see or code this. After I watched the video, I can't follow the logic to interpret the codes. This is kinda frustrating for me. : ( Even though I can 'guess' what the answer is but not very nice not to have a full understanding on this.
Thanks!
- X's value:? var f = function(a , b){ return a + b; }
var x = f("Hello", "World");
- X's value? var f = function(a, b){ var c = a + b; return c * 2; }
var x = f(3, 4);
3 Answers
Jonathan Grieve
Treehouse Moderator 91,254 PointsHi there Amber.
Let me have a go at explaining these. :)
var f = function(a , b){ return a + b; }
var x = f("Hello", "World");
With this one you have a function defined in one variable and with var x you have the function call with passes in arguments to the function.
With the function call you'd get the output HelloWorld. You might want to put a space at the end of the first string or at the beginning of the second string to get Hello World
var f = function(a, b){ var c = a + b; return c * 2; }
var x = f(3, 4);
It looks like in this one the value for x is 14.
Functions take parameters with arguments passed in at function call. The variable c holds an addition calculation which is being multiplied by 2. Just like in the first function the way the function knows how what numbers to calculate is because they're passed in at the function call.
So it's 3 + 4 which = 7 and multiply the 7 by 2.
I hope this all makes sense. :)
Jonathan Grieve
Treehouse Moderator 91,254 PointsHi Amber,
With regard to your first paragraph there are better ways to work with functions which we learn about when we get into more complex programming. I'd say if you needed to pass in more than 5 arguments (some might say more or fewer) then we probably need to look for another way to work with functions but this is the most basic level of programming with functions.
You use placeholder variables called parameters and pass in the values in as "arguments" in a function call level. In your second example, yes a is given the value of "Hello" and b is given the Value of "World." :-)
With your first paragraph... I think what you're asking is could the letters a and be in the function parameters be anything else and the answer is yes. You could actually give default values in there but really all those things are in those parentheses are placeholder variables.
var f = function(a , b){
return a + b;
}
a and b could be something like
var f = function(numberOne , numberTwo){
return numberOne + numberTwo;
}
After which you could pass in
var answer = f(2,2)
which would return 4. :-)
Amber Tai
596 PointsThanks. :D
Amber Tai
596 PointsAmber Tai
596 PointsHi Jonathan,
Thanks for the quick reply. I got the gist of what you are trying to explain. Still I'd like to ask... So, function (a, b) <-- this means: a & b or the comma 'explains' sth here, which in this case, it is the former. Does this mean, it could be: function (a, b, c, d)?
Also from both example 1 and 2 from the Quiz, how to relate var f= function (a, b) with var x = f (3, 4) or var f = function (a, b) with var x = ("hello", "world"), like how to know "hello" = a. If there are more var there, things will start to get more confusing.
Thanks.
Amber Tai
596 PointsAmber Tai
596 PointsThank you. : )
Jonathan Grieve
Treehouse Moderator 91,254 PointsJonathan Grieve
Treehouse Moderator 91,254 PointsNo problem :)