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

JavaScript

Hello guys! I would like to ask for your help.

In a "Build a Quiz Challenge Part 2 Solution" video I can't understand how the function which Dave created to convert arrays into ordered HTML lists works. He mentioned that he would create a new function that accepts an array as a parameter, then use a for loop to loop through each array item and constructing the HTML for an ordered list.

function buildList(arr) { var listHTML = '<ol>'; for (var i = 0; i < arr.length; i += 1){ listHTML += '<li>' + arr[i] + '</li>'; } listHTML += '</ol>'; return listHTML; }

My questions: Where did he define arr variable with the items? How does for loop loop through the questions? why he didn't use the question variable inside this function? And how does listHTML work with this function? Can someone please explain this function simply and stand on each details for me? I’ll really really appreciate it. Thanks a lot!

this is the linke of Build a Quiz Challenge Part 2 Solution video https://teamtreehouse.com/library/javascript-loops-arrays-and-objects/tracking-multiple-items-with-arrays/build-a-quiz-challenge-part-2-solution

post more code for this task or link to the video

2 Answers

Steven Parker
Steven Parker
243,266 Points

Here's an answer to each of your questions.

  • Where did he define arr variable with the items?

The parameter arr just represents the actual array that will be passed to the function. The function gets called on program lines 42 and 44, and the arrays passed to it are named correct and wrong. They are defined (empty) on lines 10 and 11 of the program, and questions are added to them later using the push method in the main program loop.

  • How does for loop loop through the questions?

As described at about 02:20 in the video, the loop counts from 0 to the highest index number in the array. Then that value ("i") is used as an index to select one of the array elements for each pass ("arr[i]").

  • why he didn't use the question variable inside this function?

This function will work with any array passed to it. Inside the function, the array is just named "arr". It could work with the question array, but that's not part of the objectives of this challenge.

  • And how does listHTML work with this function?

The variable listHTML is where the the function builds the string from the array. It starts by putting the opening list tag there ("<ol>"), and then adds to it for each array element. Then it finally adds the closing list tag and returns it to the caller as the function result.

Is it all clear now?

Thanks a lot! Steven Parker