我正在尝试设置一个使用递归函数nextQuestion
的计数器,但计数器score
的值会在每个循环中重置。怎么可能正确设置它?或者有更好的替代方法吗?
nextQuestion(0,0);
// i = question number
function nextQuestion(i,score) {
var currentQ = allQuestions[i];
$("#question").text(currentQ.question);
$("#score").text("Your score: " + score);
$("#quiz").submit(function(e){
e.preventDefault();
var choiceSelected = $("input[name='choices']:checked", "#quiz").val();
var answer = currentQ.correctAnswer
if (choiceSelected === currentQ.choices[answer]) {
score++;
}
i++;
if ( i >= numQuestions ) {
endQuiz();
} else {
nextQuestion(i,score);
}
});