建立JS测验,警报窗口重复以前的Q而不是问下一个Q.

时间:2016-05-16 04:18:33

标签: javascript

我试图在JavaScript中构建一个迷你测验。目前,当您正确回答问题时,测验会发出警报。除了通知用户他们是正确的之外,我希望同一个警告窗口也向用户询问下一个问题,即"你正确回答了这个问题!现在这是下一个问题......"

然而,它没有提出下一个问题,而是重复前一个问题,而这个问题刚才得到了正确回答。这是我的代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <span class="txt_b">
     <input type="number" name="" value="20" class="input_md radious_all innershadow padding_control" id ="tk" style="width:60px;" id="qty_req_<?php echo $i; ?>"/>
  </span>
</div>
<input type="hidden" name="product" id="qty_reqty_" class="product" value="qty_reqty_<?php echo $i; ?>" />

我做错了什么?

1 个答案:

答案 0 :(得分:0)

alert应该问下一个问题。 i+1

也是如此
alert("Correct! " + questions[i+1].question);

var questions = [{
    question: "What is the capital of England?",
    answer: "LONDON",
  },

  {
    question: "What is the capital of France?",
    answer: "PARIS",
  },

  {
    question: "What is the capital of Germany?",
    answer: "BERLIN",
  }
];

var correctAnswers = 0;
var response="";
for (var i = 0; i < questions.length; i++) {
  question = questions[i].question;
  answer = questions[i].answer;
  response = response!=""?prompt("Correct! "+question):prompt(question);
  if (response.toUpperCase() === answer) {
    correctAnswers++;
    if(i==questions.length-1)
      alert("Correct");
  } else {
    alert("Incorrect. The answer was " + questions[i].answer);
  }
}
html = correctAnswers + " correct answers."
document.body.innerHTML=html;