If you want to conceal the buttons, consider assigning an id to the list element:
<ul id="options">
<li>
<input type="radio" name="answer" id="answer1" class="answer">
<label for="answer1" class="radio-text" id="choice1"></label>
</li>
<li>
<input type="radio" name="answer" id="answer2" class="answer">
<label for="answer2" class="radio-text" id="choice2"></label>
</li>
</ul>
In the updateQuiz function within the else statement, add:
optionsElement.style.display = "none";
to hide the list from being displayed.
For a reset functionality, introduce a button and additional element ids to control item visibility and user choices:
<div class="row no-gutters intro-wrapper">
<div class="col-12 mt-3 my-md-5">
<div class="col-12 text-center">
<p>Some title</p>
</div>
</div>
<div class="quiz-container" id="quiz">
<div class="start-btn-wrapper">
<a class="start-btn" href="#" id="startQuiz">Start the quiz <span>►</span></a>
</div>
<div class="quiz-header">
<h2 class="text-center" id="question"></h2>
<ul id="options">
<li>
<input type="radio" name="answer" id="answer1" class="answer">
<label for="answer1" class="radio-text" id="choice1"></label>
</li>
<li>
<input type="radio" name="answer" id="answer2" class="answer">
<label for="answer2" class="radio-text" id="choice2"></label>
</li>
</ul>
</div>
<button id="submit" class="btn btn-primary">Submit</button>
<div>
<p id = "outcome"></p>
</div>
<div>
<button class="btn btn-primary" id="resetQuiz">Restart</button>
</div>
</div>
<div class="quiz-outcome" id="quizOutcome">
<div class="set-1">
<h2>Some text title1</h2>
<p>Some description etc etc</p>
</div>
<div class="set-2">
<h2>Some text title2</h2>
<p>Some description etc etc</p>
</div>
<div class="set-3">
<h2>Some text title3</h2>
<p>Some description etc etc</p>
</div>
<div class="set-4">
<h2>Some text title4</h2>
<p>Some description etc etc</p>
</div>
<div class="set-5">
<h2>Some text title5</h2>
<p>Some description etc etc</p>
</div>
<div class="set-6">
<h2>Some text title6</h2>
<p>Some description etc etc</p>
</div>
<div class="set-7">
<h2>Some text title7</h2>
<p>Some description etc etc</p>
</div>
<div class="set-8">
<h2>Some text title8</h2>
<p>Some description etc etc</p>
</div>
</div>
</div>
</div>
const quizData = [
{
question: "This is question number 1",
choices: ["Answer 1-1", "Answer 1-2"],
},
{
question: "This is question number 2",
choices: ["Answer 2-1", "Answer 2-2"],
},
{
question: "This is question number 3",
choices: ["Answer 3-1", "Answer 3-2"],
},
];
function determineOutcome(answers) {
const outcomes = {
"Answer 1-1,Answer 2-1,Answer 3-1":
"Set 1",
"Answer 1-2,Answer 2-1,Answer 3-1":
"Set 2",
"Answer 1-1,Answer 2-2,Answer 3-1":
"Set 3",
"Answer 1-2,Answer 2-2,Answer 3-1":
"Set 4",
"Answer 1-1,Answer 2-1,Answer 3-2":
"Set 5",
"Answer 1-2,Answer 2-1,Answer 3-2":
"Set 6",
"Answer 1-1,Answer 2-2,Answer 3-2":
"Set 7",
"Answer 1-2,Answer 2-2,Answer 3-2":
"Set 8",
};
const combinationKey = answers.join(",");
return outcomes[combinationKey] || "Could not determine your outcome.";
}
const questionElement = document.getElementById("question");
const optionsElement = document.getElementById("options");
const answerElements = document.querySelectorAll(".answer");
const radioTextElements = document.querySelectorAll(".radio-text");
const submitButton = document.getElementById("submit");
const startQuizButton = document.getElementById("startQuiz");
const quizBtnWrapper = document.querySelector(".start-btn-wrapper")
const resetButton = document.getElementById("resetQuiz");
const outcomeElement = document.getElementById("outcome");
resetButton.style.display = "none";
outcomeElement.style.display = "none";
// Initialize quiz index and user answers
let quizIndex = -1;
const userAnswers = [];
// Function to update the quiz content
function updateQuiz() {
quizIndex++;
if (quizIndex < quizData.length) {
const currentQuestion = quizData[quizIndex];
questionElement.textContent = currentQuestion.question;
currentQuestion.choices.forEach((choice, index) => {
radioTextElements[index].textContent = choice;
});
// Clear radio selections
answerElements.forEach((answer) => {
answer.checked = false;
console.log("is this working1");
});
} else {
optionsElement.style.display = "none";
outcomeElement.style.display = "initial";
questionElement.textContent = "Quiz Complete!";
radioTextElements.forEach((text) => {
text.textContent = "";
console.log("is this working2");
});
submitButton.style.display = "none";
console.log("is this working3");
// Determine the outcome and display it
const outcome = determineOutcome(userAnswers);
//const outcomeElement = document.createElement("p");
outcomeElement.textContent = "Your outcome: " + outcome;
//document.getElementById("quiz").appendChild(outcomeElement);
resetButton.style.display = "initial";
}
}
startQuizButton.addEventListener("click", () => {
quizBtnWrapper.style.display = "none";
updateQuiz();
});
submitButton.addEventListener("click", () => {
const selectedAnswer = Array.from(answerElements).find((answer) => answer.checked);
if (selectedAnswer) {
userAnswers.push(selectedAnswer.nextElementSibling.textContent);
updateQuiz();
}
});
resetButton.addEventListener("click", () => {
resetButton.style.display = "none";
userAnswers.length = 0;
quizIndex = -1;
updateQuiz();
quizOutcome.style.display = "none";
optionsElement.style.display = "flex";
submitButton.style.display = "initial";
outcomeElement.style.display = "none";
});