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

Finally got this to output all the messages, but lost the element. What did I do wrong:

Here is my code:

var qanda = [
["What is the capital of Illinois?", "springfield"],
["What is the last name of the governor of Illinois?", "rauner"],
["What is the Illinois State Bird", "cardinal"]
];
var rightAnswers = '';
var wrongAnswers = '';
var right = 0
var wrong = 0
function quiz() { 
for (var i = 0; i < qanda.length; i++) {
  var answer = prompt(qanda[i][0]);

  if (answer.toLowerCase() !== qanda[i][1]) {
  wrongAnswers += "<p>You got answer " +[i + 1]+ " wrong. The correct answer is " +qanda[i][1] + ".</p>";
    wrong += 1;
  } else {
    rightAnswers += "<p>You got answer " + [i + 1]+ " right! The correct answer is " +qanda[i][1] + ".</p>";
  right += 1;
  }

var messageRight = "<h2>You got " +right+ " answers right.</h2>" + rightAnswers;
var messageWrong = "<h2>You got " +wrong+ " answers wrong.</h2>" + wrongAnswers
}
 print(messageRight);
 print(messageWrong);
}
function print(message) {
  var theDiv = document.getElementById("output");
var content = document.createTextNode(message);
theDiv.appendChild(content);

  ;
}

quiz();

2 Answers

It's working just fine. Test it here: https://jsfiddle.net/8dmawhwp/

So the above solution still had the element tags and no line breaks. Here is what I came up with and it seems to work.

var qanda = [
["What is the capital of Illinois?", "springfield"],
["What is the last name of the governor of Illinois?", "rauner"],
["What is the Illinois State Bird", "cardinal"]
];
var rightAnswers = '';
var wrongAnswers = '';
var right = 0
var wrong = 0
function quiz() { 
for (var i = 0; i < qanda.length; i++) {
  var answer = prompt(qanda[i][0]);

  if (answer.toLowerCase() !== qanda[i][1]) {
  wrongAnswers += "<p>You got answer " +[i + 1]+ " wrong. The correct answer is " +qanda[i][1] + ".</p>";
    wrong += 1;
  } else {
    rightAnswers += "<p>You got answer " + [i + 1]+ " right! The correct answer is " +qanda[i][1] + ".</p>";
  right += 1;
  }

var messageRight = "You got " +right+ " answers right.";
var messageWrong = "You got " +wrong+ " answers wrong."
}
 print(messageRight, rightAnswers);
 print(messageWrong, wrongAnswers);
}
function print(message1, message2 ) {

  var h2el = document.createElement('H2');
  h2el.innerHTML = message1;

var pel = document.createElement('P');
  pel.innerHTML = message2;
   document.getElementById("output").appendChild(h2el).appendChild(pel);
}

quiz();