Before I start, this is my first time using Stack Overflow to request some help, so if I do it incorrectly I apologise and would greatly accept feedback. The communtiy here is great :)
So, my problem is that I'm creating a multiple choice quiz. My code is:
let questionList = [{
question: "What is your name?",
choices: ["bill", "bob", "steve", "joe"],
correctAnswer: "bill",
},
{
question: "What is her name?",
choices: ["dan", "danny", "danielle", "daniela"],
correctAnswer: "daniela"
},
{
question: "What is mum's name?",
choices: ["doris", "jane", "rose", "frank"],
correctAnswer: "rose"
},
{
question: "What is dad's name?",
choices: ["ian", "angus", "jack", "john"],
correctAnswer: "ian"
},
]
function callQuestions() {
for (let i = 0; i < questionList.length; i++) {
document.getElementById("quiz-questions").innerHTML = questionList[i].question;
document.getElementById("answerA").innerHTML = questionList[i].choices[0];
document.getElementById("answerB").innerHTML = questionList[i].choices[1];
document.getElementById("answerC").innerHTML = questionList[i].choices[2];
document.getElementById("answerD").innerHTML = questionList[i].choices[3];
}
}
document.getElementById("select-button").addEventListener("click", callQuestions);
In my HTML, the last question is appearing. I have a button that is supposed to call this function when I click it but it's not incrementing - it's resetting the quiz and displaying the same question.
So my questions are:
Can someone tell me why the last question is appearing and not the first? and Can someone tell me why it's not looping?
Many thanks!