I'm trying to create a trivia game that has a set of questions and answers (each question has one correct answer, but 3 other answers will be displayed in multiple choice format for a user to choose from).
The problem I'm running into is that I don't know how to best structure my objects to accomplish my goal.
I've considered the following approaches so far: -Create a 'questions' object that includes a series of questions, the correct answers, and dummy answers for each. i.e.
var metricQuestions = {
q1: {
question: "What is my name?",
correctAnswer: "Elizabeth",
incorrectAnswer1: "David",
incorrectAnswer2: "Fraser"
},
q2: {
question: "What is my dog's name?",
correctAnswer: "Annie",
incorrectAnswer1: "Purple",
incorrectAnswer2: "Face"
}
};
This approach sucks from a manual labor standpoint, because I can't pull from a bank of random 'wrong' answers when I'm presenting the user with the question and the correct answer. It feels inefficient and duplicative.
-Create separate objects for 'questions' and 'answers' respectively, and reference within the questions object which answer is correct for a particular question. Syntax wise, I couldn't get this to work.
var metricQuestions = {
q1: {
question: "What is First Contentful Paint (FCP)?",
correctAnswerID = (metricAnswers.a1)
},
q2: {
question: "What is Time to Interactive (TTI)?",
correctAnswerID: a1
}
};
I've also tried just setting the correct answer as a string within the metricQuestions object, but then I face the problem of having to deal with potential duplicates in the answer bank when I'm pulling randomly to fill in the other multiple choice options (among other potential problems).
-The final approach I've tried is a nested approach, where properties within the same object are referencing one another. Syntactically, I'm getting all sorts of errors "Uncaught SyntaxError: Invalid shorthand property initializer."
var questionBank = {
answers: {
a1: "A lab metric that measures when the main thread is quiet enough to respond to user input.",
a2: "A field and lab metric that tells when the first pixel is painted on the screen.",
a3: "Test answer 3.",
a4: "Test answer 4.",
a5: "Test answer 5."
},
questions: {
q1: {
question: "What is First Contentful Paint (FCP)?",
correctAnswerID = questionBank.answers.a1
},
q2: {
question: "What is Time to Interactive (TTI)?",
correctAnswerID = questionBank.answers.a2
},
}
};
How can I most efficiently have a bank of questions to shuffle through with a corresponding correct answer and several false answers for a multiple choice trivia game?