I have been developing a online trivia game for sometime now and this portion of the script might give you an idea how you can have 2 to 4 answers for the question.
// The `startGame` function takes an object as an argument with properties: ans1, ans2, ans3, ans4, id, question.
// These properties represent the answers to the current question, the question's id, and the question itself, respectively.
const startGame = ({ ans1, ans2, ans3, ans4, id, question }) => {
// This line sets the id of the current question to the "data-record" attribute of the element with id "currentQuestion".
document.querySelector("#currentQuestion").setAttribute("data-record", id);
// This line sets the display number of the current question (index + 1) to the text content of the element with id "currentQuestion".
document.querySelector("#currentQuestion").textContent = (index + 1).toString();
// This line sets the text content of the element with id "question" to the question itself.
document.querySelector("#question").textContent = question;
// This loop iterates over each of the answer buttons.
answerButtons.forEach((button, index) => {
// This line checks if there was a previous "pickAnswer" event listener attached to the button,
// and if so, removes it.
const previousPickAnswer = button.__pickAnswer__;
if (previousPickAnswer) {
button.removeEventListener("click", previousPickAnswer);
}
// This line creates a new "pickAnswer" event listener, and attaches it to the button.
const newPickAnswer = pickAnswer(index + 1);
button.addEventListener("click", newPickAnswer, false);
button.__pickAnswer__ = newPickAnswer;
let answerText = [ans1, ans2, ans3, ans4][index];
if (answerText) {
button.textContent = `📷 ${answerText}`;
button.style.display = "block"; // Show the button
button.style.pointerEvents = "auto"; // Enable the button
} else {
button.textContent = "";
button.style.display = "none"; // Hide the button
button.style.pointerEvents = "none"; // Disable the button
}
// If there's no corresponding answer, the button is disabled (its pointer events are set to "none").
// Otherwise, the button is enabled (its pointer events are set to "auto").
if (!answerText) {
button.style.pointerEvents = "none";
} else {
button.style.pointerEvents = "auto";
}
});
};
This pulls data in from a database table, but here’s the basics JSON layout of it
{
"id": 200,
"user_id": 2,
"hidden": "no",
"question": "Who are the two main actors in the 1987 comedy film \"Planes, Trains and Automobiles\"?",
"ans1": "Bill Murray and Dan Aykroyd",
"ans2": "Chevy Chase and John Belushi",
"ans3": "John Candy and Steve Martin",
"ans4": "Eddie Murphy and Dan Aykroyd",
"correct": 3,
"category": "movie",
"date_added": "2023-06-24 23:35:28"
}, {
"id": 199,
"user_id": 2,
"hidden": "no",
"question": "Who played the role of the inspirational teacher John Keating in \"Dead Poets Society\"?",
"ans1": "Robert Williams",
"ans2": "Robert Redford",
"ans3": "Robin Williams",
"ans4": "Robert De Niro",
"correct": 3,
"category": "movie",
"date_added": "2023-06-24 23:32:29"
}, {
"id": 198,
"user_id": 2,
"hidden": "no",
"question": "Who directed the 1939 adventure film \"Gunga Din\"?",
"ans1": "George Lucas",
"ans2": "George Stevens",
"ans3": "Howard Hawks",
"ans4": "John Ford",
"correct": 2,
"category": "movie",
"date_added": "2023-06-24 23:28:48"
}, {
"id": 197,
"user_id": 2,
"hidden": "no",
"question": "Which film won the Academy Award for Best Picture in 2020?",
"ans1": "Parasite",
"ans2": "1917",
"ans3": "Once Upon a Time in Hollywood",
"ans4": "The Irishman",
"correct": 1,
"category": "movie",
"date_added": "2023-06-24 23:26:12"
}]
Here’s a GitHub repository of the full script - brainwaveblitz.js and has other files as well.
Hope this might help a little?