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

Mayur Pande
PLUS
Mayur Pande
Courses Plus Student 11,711 Points

My solution...

slightly different, not making use of push but works nonetheless. I like his version with separation of concerns

var qAndA = [
  ["How many people live in london?", "7 million"],
  ["How many continents are there?", "7"],
  ["How many times have tottenham won the premiership?", "0"]

];
function print(message) {
  var outputDiv = document.getElementById("output")
  outputDiv.innerHTML = message;
}


function printCorrectOrPrintWrong(quest){
  var result = "";
  var correct = 0;
  var message = "";
  var correctQuestion = "<h2>You got these questions correct: </h2><ol>";
  var inCorrectQuestion = "<h2>You got these questions incorrect: </h2><ol>";
  for(var i = 0; i<quest.length; i++){
    result = prompt(quest[i][0]);
    if(result === quest[i][1]){
      correct++;
      correctQuestion += '<li>' + quest[i][0] + '</li>';
    }else{
      if(result !== quest[i][1]){
        inCorrectQuestion += '<li>' + quest[i][0] + '</li>';
      }
    }

  }
  message += "<p>You got " + correct + " question(s) right</p>";
  message += correctQuestion + "</ol>";
  message += inCorrectQuestion + "</ol>";
  print(message);
}

printCorrectOrPrintWrong(qAndA);