const questions = [{id: 0,question: "6 x 6",ans: "36",A: "48",B: "36",C: "38",D: "26"},{id: 1,question: "3 x 8",ans: "24",A: "28",B: "26",C: "22",D: "24"},{id: 2,question: "4 x 12",ans: "48",A: "60",B: "52",C: "48",D: "36"},{id: 3,question: "1 x 3",ans: "3",A: "5",B: "1",C: "4",D: "3"},{id: 4,question: "6 x 3",ans: "18",A: "18",B: "17",C: "19",D: "16"}],
[qdiv,btnnext,chk]=["question","next-question-btn","checker"].map(id=>document.getElementById(id)),
answers=document.querySelector(".answer-choices"), btns=[...answers.children];
var id=0, Q; // global variables ...
btnnext.onclick=nextQ;
answers.onclick=ev=>{ // delegate click event handler for answer buttons:
if (ev.target.textContent==Q.ans) {
chk.textContent=`Correct - ${Q.question} = ${Q.ans}!`;
btnnext.style.display=""
}
else chk.textContent="Not quite - try again!";
}
function nextQ(){
Q=questions[id];
chk.textContent="";
btnnext.style.display="none";
if (id++<questions.length){ // prepare next question
qdiv.textContent=`What is ${Q.question}?`;
[Q.A,Q.B,Q.C,Q.D].forEach((ans,i)=> btns[i].textContent=ans)
}
else { // wrap up the game:
qdiv.textContent="Game over - thanks for playing!";
answers.textContent=""
}
}
nextQ(); // show initial question
<section class="container">
<div class="title">
<h2>Multiplication Quiz 2022</h2>
<div class="underline"></div>
</div>
</section>
<section class="quiz">
<h3 id="question">Question</h3>
<div class="question"></div>
<div class="answer-choices">
<button id="choice-A" class="btn A">A</button>
<button id="choice-B" class="btn B">B</button>
<button id="choice-C" class="btn C">C</button>
<button id="choice-D" class="btn D">D</button>
</div>
<div class="checker">
<h3 id="checker"></h3>
<button id="next-question-btn" class="btn nextQuestion" style="display: none;">Next Question</button>
</div>
</section>