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

Bryn Newell
seal-mask
.a{fill-rule:evenodd;}techdegree
Bryn Newell
Full Stack JavaScript Techdegree Student 4,066 Points

Correct and Incorrect Questions are not appearing to the page.

Hi all! I've scrolled through a bunch of discussions and still cannot find what's wrong with my code. Basically my arrays for the correct and incorrect functions will not display on the page. And when I try to console.log them it seems there is still nothing in those arrays. Please help?

var correctanswers = 0;
var question;
var answer;
var response;
var html;
var questionsRight = [ ];
var questionsWrong = [ ];

var quiz = [
  ["How many States are there?", 50],
  ["What year is it?", 2018],
  ["How many legs does a spider have?", 8]
]



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 = parseInt(prompt(question));

  if (response === answer) {
    correctanswers +=1;
    questionsRight.push [question];
  }
   else {
      questionsWrong.push [question];        
     }
}

console.log(questionsRight);
console.log(questionsWrong);
html = "You got " + correctanswers + " question(s) correct.";
html += "<h2>Questions you got correct.</h2>";
html += buildList(questionsRight);
html += "<h2>Questions you got incorrect.</h2>";
html += buildList(questionsWrong);



print(html);

1 Answer

Steven Parker
Steven Parker
231,269 Points

When calling a function or method, the name must be followed by parentheses, with optional argument(s):

    questionsRight.push [question];  // original code
    questionsRight.push(question);   // fixed