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 1

Dee K
Dee K
17,815 Points

My code for Build a Quiz Challenge, Part 1.

I would like to say that so far the lessons on J/S are pretty awesome and it was made very well. I thought this challenge brought everything together that we learned so far.

Let me know what you guys think about my code.

var questionsCorrect = 'You got these questions correct:';
var questionsWrong = 'You got these questions wrong:';
var score = 0;

var answer; 
var question;

var quizQuestions = [
  [ 'Who killed Abraham Lincoln?', 'JOHN WILKES BOOTH' ],
  [ 'Which general crossed the Alps with Elephants?', 'HANNIBAL' ],
  [ 'Who lost at the Battle of Waterloo?', 'NAPOLEON' ],
  [ 'Which king separated the Church of England from the Roman Catholic Church?', 'HENRY VIII' ],
  [' Who first said "Veni, Vidi Vici?"', 'JULIUS CAESAR' ]
];

function takeQuiz ( quiz ) {
  var rightQuestion = '<ol>';
  var wrongQuestion = '<ol>';
  for ( var i = 0; i < quiz.length; i += 1) {
    question = prompt(quiz[i][0]);
    answer = quiz[i][1];
    if ( question.toUpperCase() === answer ) {
      score += 1
      rightQuestion += '<li>' + quiz[i][0] + '</li>';
    } else {
      wrongQuestion += '<li>' + quiz[i][0] + '</li>';
    }
  }
  rightQuestion += '</ol>';
  wrongQuestion += '</ol>';
  print( '<p>' + 'You got ' + score + ' question(s) right.' + '</p>' +
     '<h2>' + questionsCorrect + '</h2>' + rightQuestion + '<h2>' + questionsWrong + '</h2>' + wrongQuestion );  
}

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

alert('Are you ready to take the History Quiz? Push OK to start');

takeQuiz( quizQuestions );

Thanks!