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 trialRob Doyle
35 PointsCould this quiz be converted to multiple choice using questions with checkboxes and with more than 1 correct answer?
I am looking to build a quiz for 10 questions, but I want to provide multiple choice of 4 answers per question. One or two of the questions will have more than one correct answer. I was thinking html form and JavaScript, but I am new to programming and was looking for a tutorial on treehouse if anyone could point me in the right direction. Rob
4 Answers
Ashton Weston
3,334 PointsI think one of the easiest ways would be to give them your questions in a form so it displaces like: " What is best? A. candy B. cheese C. cats D. chocolate" Then have it so they enter the letter A, B, C, or D. Like so.
var questions = [
['What is a very popular pet? A. Cat B.Dog C. Parrot D. Fish', 'A', 'B'],
['When did Apollo 11 land on the moon? A. 1960 B. 1966 C. 1969 D. 1970', 'C']
];
for (i = 0; i < questions.length; i += 1){
var answer = prompt(questions[i][0]);
if ( answer == questions[i][1] || questions[i][2]){
document.write('<h1>' + answer + ' was correct!' + '</h1>');
} else {
document.write('<h1>' + answer + ' was wrong! Instead the answer was, ' + questions[i][1] +'</h1>');
}
}
This should work for what you want to do. Just as a note if you did not know || means "or" in JavaScript.
Rob Doyle
35 PointsThanks Ashton I've been struggling with the concept of this as I am just starting to learn about arrays. The ultimate way I would want the quiz to look is to use check boxes instead of a prompt box but also grade the test at the end including a solution button to show the answers. I am a beginner and I can see how code can get very complex very quickly. Thanks again. I really appreciate the help. Rob
Rob Doyle
35 PointsHi Ashton Just one other thing. If all my answers to a question was correct, would I have to alter the if statement? thanks Rob
Ashton Weston
3,334 PointsNo problem as you can tell I am only just learning the basics myself. As for your question for all the questions being right you would just have to add extra "OR's". So the if would be changed to. if ( answer == questions[i][1] || questions[i][2] || questions[i][3] || questions[i][4]){ or however many you need.
Rob Doyle
35 PointsThank you for the help. I'm still trying to find the best way to learn Javascript. So many different approaches from books to videos. Reverse engineering existing code seems to help to see the bigger picture.