I am creating a quiz in JS and the questions need to show only one at a time. When I choose an answer choice, I am notified "correct" or "wrong", then it automatically goes to the next question.
var questions = [
{
question: "1. question 1 here",
answers: [
"1a here",
"1c here",
"1d here"
],
correctanswer: "1b here"
},
{
question: "2. question 2 here",
answers: [
"2a here",
"2b here",
"2c here",
"2d here"
],
correctanswer: "2a here"
},
{
question: "3. question 3 here",
answers: [
"3a here",
"3b here",
"3c here",
"3d here"
],
correctanswer: "3d here"
},
]
function startQuiz() {
var output =[];
var answers;
for (var i=0; i < questions.length; i++) {
answers =[];
for(letter in questions[i].answers) {
answers.push(
'<label>'
+ '<input type="radio" name="question' +i+'"value="'+letter+'">'
+ letter + ': ' + questions[i].answers[letter] +
'</label'
// var userAns = document.getElementById("quiz")
// userAns.innerHTML = questions[i].question + "<br>" + questions[i].answers;
);
}
output.push(
'<div class="question">' + questions[i].question + '</div>'+
'div class="answers">' + answers.join('') + '</div>'
);
// //if right or wrong
// if (userAns === questions.correctanswer) {
// var grade = document.createElement("p");
// grade.textContent("Correct!");
// grade.className("grade");
// grade.setAttribute("grade-id");
// } else {
// grade.textContent("Wrong!");
// timerRef - sec *10;
// }
}
quizContainer.innerHTML = output.join('');
}
startButton.addEventListener('click', startTimer);
startButton.addEventListener('click', startQuiz);
I commented out some lines because I'm not sure how to get them to work. I tried making the answer choices radio buttons - the questions should only show up when 'start' is selected.