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 trialDavid Orlow
Courses Plus Student 1,333 PointsJavaScript Parameter question
I am stuck on this question...
Call the function by giving an age in the space for a parameter.
function random_age(age) {
Math.floor(Math.random() * age);
}
random_age();
Exactly how it is above is the code given to me that is wrong and I need to fix it. The parameter is function random_age(age) the word age between the parentheses, correct? So, as I see it, it appears already correct to me. But I've tried putting age between other parenthesis, all of the parenthesis and not changing it at all and nothing is taking. Can someone explain this to me?
2 Answers
Sun-Li Beatteay
10,606 PointsThe "calling" of a function is done after you set the function. So in your above code, the function is set:
function random_age(age) {
Math.floor(Math.random() * age);
}
and to call it, you do:
random_age("insert age here");
So the question is asking you merely to put in an age number where I wrote the "insert age here" because that is your parameter. If you create a function that requires a parameter, you cannot call it without giving it that parameter. In this case, it's an age. S
So one answer could look like this:
random_age(5);
Jennifer Nordell
Treehouse TeacherSun-Li Beatteay gave a very nice answer! However, I'm going to point out some semantic differences. Some programmers use the terms "argument" and "parameter" interchangeably. However, this is not entirely correct in the strictest sense. The parameter is in the declaration and definition of the function. The argument is what goes into the parameter when you call (invoke/execute) the function. Here's an example:
function addTwo (x, y) {
return x + y;
}
var result = addTwo(5, 20);
console.log(result);
In this example, the function has two parameters x
and y
. And it accepts two arguments when we call the function. The arguments are 5 and 20. Try and think of it this way: a parameter is what the function accepts as information coming in. The arguments are the information sent to the function when we call it.
Hope this helps!
Sun-Li Beatteay
10,606 PointsThanks Jennifer for clarifying that! I do interchange them too often. You're very correct that they are different.