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

Seokhyun Wie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Seokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,606 Points

Can anyone give a comment on my coding compared to the one that is shown on the video?

I really don't know what would be the pros and cons of my coding compared to one on the video. I would be a great help if anyone can comment on this. Thank you so much in advance! (anything apart from document.write();)

function print(message) {
  document.write(message);
}

// Create Arrays
let quiz = [
  ['What year you two met?', '2017'],
  ['What is your favorite pet?', 'cat'],
  ['How many legs does an insect have?', '6']
]
// To count the number of correct/wrong answers
let guessCorrect = 0;
let guessWrong = 0;
// To pile up questions that are right or wrong
let correctAnswer = '';
let wrongAnswer = '';

// Create a Loop
for (let i = 0; i < quiz.length; i += 1) {
  let ask = prompt(quiz[i][0]);
  if (ask.toLowerCase() === quiz[i][1]) {
    correctAnswer += '<li>' + quiz[i][0] + '</li>';
    guessCorrect += 1;
  } else {
    wrongAnswer += '<li>' + quiz[i][0] + '</li>';
    guessWrong += 1;
  }
}

print('You got ' + guessCorrect + ' question(s) right.')
print('<h2>You got these questions correct:</h2>' + '<ol>' + correctAnswer + '</ol>');
print('<h2>You got these questions wrong:</h2>' + '<ol>' + wrongAnswer + '</ol>');