JSFiddle - React, Tailwind, and code Playground
HTML
<button id="nextQstBtn">Next, plz!</button>
<div id="questionHolder"></div>
JavaScript
var myGlobalVar = {
singleQuestion: 0,
nextButton: document.getElementById("nextQstBtn"),
questionHolder: document.getElementById("questionHolder"),
allQuestions: [{
question: "This is question number one",
choices: ["one", "two", "three", "four"],
correctAnswer: "two"
}, {
question: "This is question number two",
choices: ["dog", "cat", "bear", "lion"],
correctAnswer: "bear"
}, {
question: "This is question number three",
choices: ["Moon", "Sun", "Mars", "Snickers"],
correctAnswer: "Sun"
}],
clearNode: function(node) {
while(node.hasChildNodes()) {
node.removeChild(node.firstChild);
}
},
switchQuestion: function () {
myGlobalVar.singleQuestion += 1;
if (myGlobalVar.singleQuestion === myGlobalVar.allQuestions.length) {
myGlobalVar.singleQuestion = 0;
}
myGlobalVar.showQuestion(myGlobalVar.singleQuestion);
},
showQuestion: function (qstNum) {
var qstText = myGlobalVar.allQuestions[qstNum].question,
qstTextNode = document.createTextNode(qstText),
qstHeader = document.createElement("H1"),
choices = myGlobalVar.allQuestions[qstNum].choices;
qstHeader.appendChild(qstTextNode);
myGlobalVar.showQstData(qstHeader, choices);
},
showQstData: function (qstHeader, choices) {
myGlobalVar.clearNode(myGlobalVar.questionHolder);
var fragment = document.createDocumentFragment(),
answersHeader = document.createElement("H2"),
answersHText = "Possible answers:",
answersHTextNode = document.createTextNode(answersHText),
ol = document.createElement("OL");
answersHeader.appendChild(answersHTextNode);
for (var c in choices) {
var newLi =...