I am working on a JavaScript/HTML based quiz in my free time, however I have ran into an issue: It goes up to question 2 then it doesn’t show the next question, I have checked the console for any errors but there are none to be seen.
I apologise that it is not the most complex code, I am still learning JS. If anybody could point me to where I went wrong it would be great!
var questions = ["What one of these New Zealand birds is dubbed 'Owl Parrot'","Q2","Q3"]; var answers = { 1:{ 1:"Kea", 2:"Kakapo", 3:"Kiwi", 4:"Takahe", c:2 }, 2:{ 1:"DEBUG", 2:"DEBUG", 3:"DEBUG", 4:"DEBUG", c:4 }, 3:{ 1:"DEBUG2", 2:"DEBUG2", 3:"DEBUG2", 4:"DEBUG2", c:1 } }; var score = 0; var question = -1; function play(){ question = question + 1; askquestion(question); } function askquestion(q){ console.log(q); document.getElementById("StartButton").style.display = "none"; document.getElementById("Question").innerHTML = questions[q]; document.getElementById("Answer1").innerHTML = answers[q+1][1]; document.getElementById("Answer2").innerHTML = answers[q+1][2]; document.getElementById("Answer3").innerHTML = answers[q+1][3]; document.getElementById("Answer4").innerHTML = answers[q+1][4]; } function answerquestion(a){ console.log(a); if (answers[question+1]["c"] == a){ document.getElementById("results").innerHTML = "Correct!"; }else{ document.getElementById("results").innerHTML = "Incorrect!"; } if (question + 1 < questions.length + 1 ){ console.log(question+1,questions.length); askquestion(question+1); }else{ question = -1; } }
Sorry if this post was formatted wrong, it is my first time on this site.
Advertisement
Answer
You never update the value of your question
variable. That keeps your state.
In your answerquestion
method you can have:
... if (question < questions.length ){ question = question+1 console.log(question,questions.length); askquestion(question); }