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 Solution

A question about Dave's solution, and an analysis of mine

First of all, where is Dave's script getting its questions from? As far as I can tell, he initializes the questions variable, but doesn't actually fill it with an array.

Second of all, this is my solution to the quiz problem - and I see that its quite different from Dave's. Where did I go wrong? Dave's seems much simpler, as opposed to mine. How could I shorten it or otherwise improve it?

 var questionsAnswers = [
  ['What is my first name?', 'nir'],
  ['How old am I?', 25],
  ['What city do I live in?', 'new york'],
  ['What country was I born in?', 'america']
];
var numCorrect = 0;
var listRight = '<h1>You got these questions correct:</h1><ol>';
var listWrong = '<h1>You got these questions wrong:</h1><ol>';
var guess;

//Begin Quiz Loop
for (var i = 0; i < questionsAnswers.length; i++) {
  guess = prompt(questionsAnswers[i][0]);
  if (isNaN(guess)) {
    guess = guess.toLowerCase();
  } else {
    guess = parseInt(guess);
  }
  if (guess === questionsAnswers[i][1]) {
    numCorrect += 1;
    listRight += '<li>' + questionsAnswers[i][0] + '</li>'
  } else {
    listWrong += '<li>' + questionsAnswers[i][0] + '</li>'
  }
}

listRight += '</ol>';
listWrong += '</ol>';

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

print('You got ' + numCorrect + ' out of ' + questionsAnswers.length + ' questions right!');

if (numCorrect > 0) {
    print(listRight);
}

if (numWrong > 0) {
    print(listWrong);
}

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

I haven't attempted the challenge yet as there's a problem with workspaces but as far I can tell you've nailed down everything you need to do including capturing incorrect input.

I assume your script works? Don't worry if it's different to Dave's solution. There are often many ways of solving one programming problem. :-)

Yup, it works! Thanks for your answer. As a follow-up question (if I may), I continued watching the videos and attempted to use Dave's newer print(message); function, which utilizes document.getElementByID() and .innerHTML. What happened was, only the wrong questions got printed out.

After some googling I simply changed:

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

to:

//code removed for brevity...
  outputDiv.innerHTML += message;
}

But I also came across appendChild(). Is there a reason Dave doesn't get into appendChild()? Is it reserved for a different course?