递归Javascript计数器

时间:2014-05-23 22:34:08

标签: javascript recursion

我正在尝试设置一个使用递归函数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);
      }
    });

0 个答案:

没有答案