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

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);