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

Javascript Challenge Quiz

My solution to the quiz challenge. Functions and arrays are great!

function ListItems(QuizList, List) {
  var ReturnList = "<ol>";
  for (var a = 0; a < List.length; a += 1) { ReturnList += "<li>" + QuizList[ List[a] ][0]+ "<br>" + QuizList[ List[a] ][1] + "</li>"; }
  ReturnList += "</ol>"
  return ReturnList;
}

function Quiz() {
  var Answer;
  var CorrectAnswers = [];
  var WrongAnswers = [];
  var QuizList = [
  ["Howmany provinces does Belgium count?", "10"],
  ["Howmany regionale gouverments does Belgium have? (without the Federale gouverment)", "6"],
  ["Howmany languages does the gouverment of Belgium apply?", "3"],
  ["What is both the capital of Belgium and of Europe?", "brussels"],
  ["Which city of Belgium is considered the 3rd biggest harbour port in Europe?", "antwerps"],
  ["Which medieval city is known for choclate?", "bruges"],
  ["For what other product is Belgium known for, except for choclate and (french) frites?", "beer"]];
  for (var a = 0; a < QuizList.length; a += 1) { 
    Answer = prompt(QuizList[a][0]);
    if (Answer.toLowerCase() === QuizList[a][1]) { CorrectAnswers.push(a); }
    else { WrongAnswers.push(a); }
  }
  var message = "<p>You have " + CorrectAnswers.length + " answers right out of " + QuizList.length + ".</p>";
  message += "<h2>Good answers:<h2>" + ListItems(QuizList, CorrectAnswers);
  message += "<h2>Wrong answers:<h2>" + ListItems(QuizList, WrongAnswers);
  print(message);
}

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

Quiz();

1 Answer