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

no pop up window shows up??

var questions = [
  ['How many states are in the United States?', 50],
  ['How many continents are there?', 7],
  ['How many legs does an insect have?', 6]
];
var correctAnswers = 0;
var question;
var answer;
var response;
var correct=[];
var wrong=[];

function print(message) {
  document.write(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 < questions.length; i += 1) {
  question = questions[i][0];
  answer = questions[i][1];
  response = prompt(question);
  response = parseInt(response);
  if (response === answer) {
    correctAnswers += 1;
    correct.push(question);
  } else{
    wrong.push(question);
  }
}

html = "You got " + correctAnswers + " question(s) right."
print(html);
html+="<h2> you got this questions correct:</h2>";
html+= buildList(correct);

you have some issues with your buildList function. I am not sure what you are trying to accomplish in it, but you have an extra ":

 var listHtml=""; for var i=0; i" 

also that is not the syntax for a for loop:

var i=0; i" + arr[i] +""; }

the loop should be something like this:

for(var i=0; i < arr.length; i++) {
//do stuff
}

1 Answer

Hey Britta,

There were some issues with your code. As Jason pointed out, your for statement needed slightly redone. After the edit, you only needed parenthesis surrounding var i=0; i<arr.length; i+=1.

Also, you should not use the function print() because it is much slower than just calling document.write() itself and it causes issues with Safari because print() is a function for printing information from a page in Safari.

I also redid the bottom HTML and added a wrongAnswers variable, so that you can track wrong answers as well as right ones.

var questions = [
  ['How many states are in the United States?', 50],
  ['How many continents are there?', 7],
  ['How many legs does an insect have?', 6]
];
var correctAnswers = 0, wrongAnswers = 0;
var question, answer, response;
var correct=[], wrong = [];

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 < questions.length; i += 1) {
  question = questions[i][0];
  answer = questions[i][1];
  response = parseInt(prompt(question));
  if (response === answer) {
    correctAnswers += 1;
    correct.push(question);
  } else {
    wrongAnswers += 1;
    wrong.push(question);
  }
}

html = "<h2>You got " + correctAnswers + " question(s) right.</h2>";
html += "<p>You got these question(s) correct:</p>" + buildList(correct);
html += "<h2>You got " + wrongAnswers + " question(s) wrong.</h2>";
html += "<p>You got these question(s) wrong:</p>" + buildList(wrong);
document.write(html);