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

Scott Walton
Scott Walton
9,174 Points

My final message isn't working as I'd expect. Any help would be greatly appreciated.

My rightQuestions and wrongQuestions arrays are not displaying in the final message. Though when I test to see if they're being logged to the console they appear. My correct questions variable isn't being added too either. I'm confused. Thank you for your time.

function print(message) {
  document.write(message);
}
// main function to ask questions and record answers
function quizTime( quiz ) {
    for (var i = 0; i < quiz.length; i += 1) {
   var answer = prompt (quiz[i][0]);
   if ( answer.toLowerCase() === quiz[i][1] )  {
    rightQuestions.push(quiz[i][0]);
    correct += 1;
  } else {
    wrongQuestions.push(quiz[i][0]);
    }
  }
}

// array of questions and answers
var questions = [
  ["Test?", "hehe"],
  ["Correct?", "haha"],
  ["Wrong?", "hoho"]
];

// variables and arrays
var rightQuestions = [];
var wrongQuestions = [];
var correct = 0;



// message to write to the player. Displaying questions right and wrong and how many correct
var message = ("You got " + correct + " questions correct out of " + questions.length );
message += ("<h2>You got these questions correct: </h2> " + rightQuestions.join(", "));
message += ("<h2>You got these questions wrong: </h2>" + wrongQuestions.join(", "));



// call functions
quizTime(questions);
print(message);

// console.log to see if they are being stored. They are and i'm confused!
console.log(wrongQuestions);
console.log(rightQuestions);

1 Answer

Justin Kraft
Justin Kraft
26,327 Points

Move your quizTime() method call above your message variable and it should print what is expected.

Scott Walton
Scott Walton
9,174 Points

Thank you very much.