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

Trying to solve this problem a different way.

My code works the way it should, but I would like for it to show the questions I got right and the ones I got wrong vertically. I actually had it solved the other day the way I have written it without a function buildList, but I can't remember what I did because I forgot to save my work. I have been trying to figure it out and I am at a loss. Any help on this would be greatly appreciated.

Here is my code:

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 incorrectAnswers = 0; 
var correct = [];
var incorrect = [];

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;

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;
             } else {
incorrectAnswers += 1;
document.write("<h2>You got " + correctAnswers +  " question(s) correct.</h2>");
document.write("<h2>You got these question(s) correct:</h2>");
document.write("<h2>You got " + incorrectAnswers +  " question(s) incorrect.</h2>"); 
document.write("<h2>You got these question(s) incorrect:</h2>");

Moderator Edited: Added Markdown to code for readability.

Jesus Mendoza
Jesus Mendoza
23,289 Points

Create a new array and if the response equals the answer then push correct to the new array, else push false

I am not quite sure what you mean by creating a new array or do I just need a new for/else statement. Also, where in the code would it go?

1 Answer

Andrew Liu
Andrew Liu
2,357 Points

If I understand your question correctly, I think you could just replace



correct.push(question + '<br>');

and likewise for incorrect.

That worked. I don't think that was what I did before, but it worked as I expected. Thank you so much for your help. It was greatly appreciated.