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

Austin Miller
Austin Miller
5,171 Points

Correct/wrong questions are not printing correctly

This code is only displaying the last item that was put into the correct array and wrong array when it comes to displaying the questions the user gets correct/wrong.

let questions = [
  ["What programming language is this?", "javascript"],
  ["Which state is the closest to a square?", "colorado"],
  ["Who is teaching this lesson?", "dave mcfarland"]
];
let correctAnswers = 0;
let question;
let answer;
let response;
let html;
let correct = [];
let wrong = [];

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

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

for (let i = 0; i < questions.length; i++) {
  question = questions[i][0];
  answer = questions[i][1];
  response = prompt(question);
  response = response.toLowerCase();
  if (response === answer) {
    correctAnswers++;
    correct.push(question);
  } else {
    wrong.push(question);
  }
}

html = `You got ${correctAnswers} question(s) correct.`;
html += '<h2>You got these questions correct:</h2>';
html += buildList(correct);
html += '<h2>You got these questions wrong:</h2>';
html += buildList(wrong);
print(html);

1 Answer

Hi Austin Miller,

I believe the issue you’re running into is that within the for...loop in your buildList function, you’re setting listHTML equal to instead of listHTML plus equal to.

Try making that change, and see if that fixes the problem you’re having. If not, reach back out and I’ll take a deeper look into what’s going on.

Austin Miller
Austin Miller
5,171 Points

Thank you, Brandon! That was the problem.