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

Brad Horvath
Brad Horvath
3,845 Points

Need help with quiz challenge

var quiz = [
  ["How presidents have there been?",44],
  ["How many states are there?",50],
  ["How many stripes are on the American Flag?",13]
];
var correctQuestions = [
];
var incorrectQuestions = [
];
var correctAnswers = 0;
var question;
var answer;
var response;
var html;
function print(message) {
  var outputDIV = document.getElementById("output");
  outputDIV.innerHTML = message;
}

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

for (var i = 0; i < quiz.length; i += 1) {
  question = quiz[i][0];
  answer = quiz[i][1];
  response = prompt(question);
  response = parseInt(response);
  if (response === answer) {
    correctAnswers += 1;
    correctQuestions.push(question);
  } else {
    incorrectQuestions.push(question);
  }
}
html = "You got " + correctAnswers + " question(s) right.";
html += "<h2>You got these questions correct:</h2>";
html += buildList(correctQuestions);
html += "<h2>You got these questions wrong:</h2>"
html += buildList(incorrectQuestions);
print(html);

I am doing the js quiz challenge and I am experiencing some problems. If someone could help me, that would be great. We need to prompt some questions and then see if they are correct. I have that done correctly expect it asks each question twice. Also, I need to have the questions answered correctly appear in a certain area and the questions answered incorrectly appear in a different area. If someone can help me, that would be great.

2 Answers

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

Hi, Brad. I tested your code in the browser console, and it works fine for me. The only thing is that you have

listHTML = "</ol>";

inside your buildList function, but it should be this:

listHTML += "</ol>";

so it's overwriting the variable instead of concatenating.

Brad Horvath
Brad Horvath
3,845 Points

Yes. You were correct. Just a typo. Thanks for your help