Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 2 Solution

Getting "undefined" list items where there should be nothing listed..

My code is running correctly EXCEPT it will create three list items under both the correct/incorrect sections and name then "undefined".. Any ideas on what I have incorrect here? I know it is messier than he explained, I wanted to take a shot at things before I saw his solution.

let quiz = [ 
  [`What is my first name?`, 1],
  [`What is my husband's name?`, 2],
  [`Is it faster to Beijing or by bus?`, 3]
];

let correctQ = [];
let incorrectQ = [];

let correctAnswers = 0;
let question;
let answer;
let response;
let html;


function print(message) {
  let outputDiv = document.getElementById("output");
  outputDiv.innerHTML = message;
}

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


for (i=0;i<quiz.length;i++) {
  question = quiz[i][0];
  answer = quiz[i][1]
  response = Number(prompt(question));
  if (response === answer ){
    correctAnswers += 1;
    correctQ.push(question);
  } else {
    incorrectQ.push(question);
  }
}

html = "You got " + correctAnswers + " question(s) correct!";
print(html);

html += `<h2>You answered these questions correctly:</h2>`
html += buildList(correctQ);
html += `<h2>You answered these questions incorrectly:</h2>`
html += buildList(incorrectQ);
print(html);