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

Daniel Salvatori
Daniel Salvatori
2,658 Points

Please review my code and offer Improvements

So I was able to get the page to show which questions were correct and which were wrong, however my formatting is a bit wonky and I feel like my code might be too hard to read.

Any suggestions?

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 wrongAnswers = 0;
var question;
var answer;
var response;
var html;

var correctArray = [];
var wrongArray = [];


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;
   var answerCorrect =  correctArray.push(questions[i][0] + "<li>")
  } else {
    wrongAnswers +=1
   var answerWrong = wrongArray.push(questions[i][0] + "<li>")
  }
}



html = "<h2>You got " + correctAnswers + " question(s) right.</h2><ul><li>" + correctArray + "<br><br>" + "<h2>You got " + wrongAnswers + " question(s) wrong.</h2><ul><li>" + wrongArray + "<br>"
print(html);

1 Answer

Ken S.
Ken S.
3,838 Points

Hi Daniel,

Your var answerCorrect and var answerWrong inside your loop is odd, those variables are never used so why create them at all, I would remove them and just have the push method executed to push the correct or wrong answer into their respective arrays. I would also remove the <li> tags from the push contents, because you are pushing the <li> into your array which is keeping track of correct and incorrect answers. Stick to only pushing the question, so you can manipulate it later. I just think it is cleaner and easier to work with later this way.

At the end, don't forget to close your <ul> and <li> tags in appropriate places. By closing those tags at the right places, you should be able to avoid using the <br> tag at all.

Finally, when you simply place your 'correctAnswers' into the output, if you took my advice and removed the <li> tags, you'll need to format them again. You could try another loop to go through your correctAnswers and wrongAnswers newly populated arrays. I created a function to print the contents of each array with formatting, since I did it twice (once for correct, once for incorrect answers array).

Hope this gives you some hints to improve your code without just simply giving it away. I'm not trying to be mean, but I truly believe figuring it out on your own will result in more learning.

Also, I'm only a student, so my advice may not be perfect. Take it with a grain of salt, and sorry if it is not clear with examples. Good luck!