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 trialKishan P
9,921 Pointsneed Help with the parameter in function ?
function buildlist(array) {
var listHTML = '<ol>';
for (var i = 0; i < array.length; i++) {
listHTML += '<li>' + array[i] + '</li>';
}
listHTML += '</ol>';
return listHTML;
}
So what exactly does the (array or any name) parameter does ... like i am confuse with how does parameter (array) pass to correct answers and then print the right stuff..
2 Answers
benyoungblood
10,062 PointsHi Kishan,
The array parameter is just a representation of whatever list you are passing to it. So say for example we have a list of sports. Let's call our list 'sports'. Our 'sports' list includes 'baseball', 'football', 'soccer', and 'basketball'.
If we want to output our list on our website, then we can just use our buildList function. So we know what our buildList function does, it takes a list and it formats it to display on our website, but how does it know what list to display and which items from that list to display? Well, it knows based on the list we specify for the array parameter.
When we go to display the list, we will call buildList by doing this:
buildList(sports);
Sports is an array that we have already created at some point in time. When we pass the sports array in buildList, it looks at the sports array and can see the amount of items that are inside. In our case, there are 4 items in our sports array, so it will iterate through the length of our sports array and display them as list items. Below is an example of how this would look.
const sports = ['baseball', 'football', 'soccer', 'basketball'];
function buildList(array) {
var listHTML = '<ol>';
for (var i = 0; i < array.length; i++) {
listHTML += '<li>' + array[i] + '</li>';
}
listHTML += '</ol>';
return listHTML;
}
buildList(sports);
Our output would be something like this:
- baseball
- football
- soccer
- basketball
Jonathan Grieve
Treehouse Moderator 91,253 PointsHi Kishan,
A parameter is just a variable that you can pass into a function so you can pass information in that is "local" to the function. It can only be used by that function.
The way you actually give the value of that value is by way of a function "call".
//function call
buildlist(list[1,2,3,4])
Kishan P
9,921 Pointssorry but how does the array parameter knows the length of the questions ? i am so confuse