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

My first attempt (not refactored to make it modular)

Here's one way I tried to solve the Quiz coding challenge. My next attempt will be to make it more modular. Suggestions on ways to make this better are welcome. :)

var score = 0;
var correctMsg = "<h2>You got these questions right: </h2> <ol>";
var incorrectMsg = "<h2>You got these questions wrong: </h2> <ol>";
var yourScore = " ";

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


var quiz = [
  ["What are non-wizarding folk called?", "muggles"],
  ["What are the soul sucking, despair inducing creatures called that can be vanquished with a Patronus charm?", "dementors"],
  ["What are the objects into which you can split your soul?", "horcruxes"]
];



//ask a question and add it into a loop
//compare the answer and see if answer was correct.
for (i = 0; i < quiz.length; i++){
  var answer = prompt(quiz[i][0]);

  if (answer === quiz[i][1]){
    correctMsg += "<li>" + quiz[i][0] + "</li>";
    score += 1; 
  } else {
    incorrectMsg += "<li>" + quiz[i][0] + "</li>";
    //document.write("try using plurals");
  }
}
incorrectMsg += "</ol>";
correctMsg += "</ol>";
yourScore += "<b>Your Score is : </b>" + score; 
print (yourScore);

//if you got 0 questions correct, then don't print this section
//if you got 0 questions wrong, then don't print out this section 

if ( score === 0 ) {
  print(incorrectMsg);
} else if (score < quiz.length - 1) { 
  print(correctMsg);
  print(incorrectMsg);
} else {
  print(correctMsg);
}