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 JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 2 Solution

Vlad Legkowski
Vlad Legkowski
9,882 Points

function buildList(arr)

Can someone please explain me how function buildList(arr) accessing var question array?

http://codepen.io/anon/pen/BKmLeN?editors=0010

Can somebody explain it a bit more deeper? Apart of the arr question, this listHTML, where do we use it? I understand that it builds the unordered list for us, but where to we return(listHTML); ?

I am not even sure this questions make sense. :(

Vlad Legkowski
Vlad Legkowski
9,882 Points

Why dont we use something like buildList(questions); as we did in previous lessons? Like it is explained by Dave here https://teamtreehouse.com/community/how-is-songs-influencing-the-var-playlist

1 Answer

Stephen Gheysens
Stephen Gheysens
11,935 Points

Hi Vlad,

The buildList() function behaves very similarly to the buildList function in the previous lessons. In the Codepen, you have a few parts to your program:

  1. You declare eight variables and define a few of them
  2. Your print() function
  3. Your buildList() function
  4. A for loop that accesses the elements in the questions array. I've commented the loop below so yo know what's happening on each line. Imagine the 2-dimensional array looks like this:
                          0                     1
questions =    0   [ 'Capital of Russia',     'moscow' ],
               1   [ 'Capital of France',      'paris' ],
               2   [ 'Capital of UK',         'london' ] 

In the for loop below, 'i' goes from 0 to 1 to 2, then exits the loop because it's no longer < questions.length (which is 3)

// for the length of questions, starting with i = 0, loop until i >= questions.length
for (var i = 0; i < questions.length; i+= 1) {
  // 'question' (the current question) is located in 'questions' at i (the current index), and is in i at 0 (the first element of the array in the current index i)
  question = questions[i][0];
 // 'answer' (the current answer) is located in 'questions' at i (the current index), and is in i at 0 (the second element of the array at i)
  answer = questions[i][1];
  // this prompts the user and records the value in 'response'
  response = prompt(question);
  // sets 'response' to all lower case because that's how the answers are stored in 'questions'
  response = response.toLocaleLowerCase();
  // if the response and the current answer are equal...
  if (response === answer) {
    // increment the correctAnswers counter...
    correctAnswers += 1;
    // and push the question onto the correct questions array
    correct.push(question);
  } else {
    // otherwise add it to the wrong questions array
    wrong.push(question);
  }
}

Later, buildList() is called on the correct questions array and the wrong questions array to add ordered lists (<ol>) for each to the string variable 'html'. 'html' is then passed to the print() function and the end result is supposed to be output to the innerHTML of a div with id #output.