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

My Attempt - Any feedback on how to improve would be awesome.

Here's my first attempt to the challenge. Any feedback would be awesome.

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

// My three questions
var questionList = [['Who plays the 10th doctor?', 'david tennant'],
               ['Who plays the 11th doctor?', 'matt smith'],
               ['Who plays the 12th doctor?', 'peter capaldi']
              ];

// Variable declarations
var response;   
var correctCount = 0;  
var correctQuestions = [];
var incorrectQuestions = [];

// Question prompt & loop
for ( var q = 0; q < questionList.length; q += 1 ) {
  response = prompt(questionList[q][0]);
    if ( response.toLowerCase() === questionList[q][1] ) {
    correctQuestions.push(questionList[q][0]);
      correctCount += 1;
  } else {
    incorrectQuestions.push(questionList[q][0]);
  }
}

// Organise answered questions into respective lists. 
function printList (list) {
  var listHTML = '<ol>';
  for ( var i = 0; i < list.length; i += 1) {
    listHTML += '<li>' + list[i] + '</li>';
  }
  listHTML += '</ol>';
  print(listHTML);
}

// Print out results
print('You got ' + correctCount + ' question(s) right. <br>');
print('<h2>You got these questions correct:</h2>');
printList(correctQuestions);
print('<h2>You got these questions wrong:</h2>');
printList(incorrectQuestions);

1 Answer

I think that is great. But then again, I just did the challenge myself. :) Let's see what somebody who has done it longer has to say.

I see you used .push for your correct and wrong questions. I did it with concat and it works too. :)