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

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

Brandon White
seal-mask
MOD
.a{fill-rule:evenodd;}techdegree seal-36
Brandon White
Treehouse Moderator

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.