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

Mine solution for quiz challenge

// Quiz Builder

// quiz array data structure var quizQuestions = [ ['Capital of Pakistan?', 'islamabad'], ['In which year Pakistan got independence?', '1947'], ['Current PM of Pakistan?', 'imran khan'] ];

// array that will hold the answers provided by the user through prompt box var answers = [];

// array that will hold that questions that was guessed right by the user var correc_answers = [];

// array that will hold that questions that was guessed wrong by the user var wrong_answers = [];

// generalized function to print any message to the document function printMessage(message) { document.write('<p>' + message + '</p>'); }

// function that will print the contents of array (here both arrays one by one that holds wrong and correct answers function printQuestions(questions) { for(var i = 0; i < questions.length; i++) { printMessage('<p>' + questions[i] + '</p>'); } printMessage('<br>'); }

// loop that prints question to the user and get input and store them in answers array for(var i = 0; i < 3; i++) { answers[i] = prompt(quizQuestions[i][0]).toLowerCase(); if(answers[i] === quizQuestions[i][1]) { correc_answers.push(quizQuestions[i][0]); } else { wrong_answers.push(quizQuestions[i][0]); } }

// finally prinitng correct questions printMessage('<h2>Questions you got right</h2>'); printQuestions(correc_answers);

// finally prinitng wrong questions printMessage('<h2>Questions you got wrong</h2>'); printQuestions(wrong_answers);

1 Answer

Steven Parker
Steven Parker
231,261 Points

Just sharing, or did you have a question about it?

And when posting code, use Markdown formatting to preserve the appearance. Instructions can be found in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:

Or for more detailed instructions, you can watch this video on code formatting.