需要帮助:随机进行测验

时间:2014-03-16 13:18:31

标签: javascript

我无法随机提问。如果你能提供帮助,那就太棒了!

(如果你有长矛时间;我已经被赋予了用绿色羽毛标记正确答案的任务和红色羽毛的错误答案,例如,如果你得到3个正确和2个错误。它将显示3绿色的羽毛和2根红色的羽毛作为分数。)谢谢!

<script type="text/javascript">

var questions = [
    ['firstcar.gif','0'],
    ['secondcar.gif','1'],
    ['thirdcar.gif','2'], 
    ['firstcar.gif','0'],
    ['secondcar.gif','1'],
    ['thirdcar.gif','2'] // Note: no comma after last entry
];
var qNo = 0;
var correct = 0;
var cnt = 0;

function NextQuestion(response) {
  if ((qNo < questions.length) && (response == questions[qNo][1])) { 
        correct++; 
}
  document.getElementById('score').innerHTML = 'Correct ' + correct + ' of 6 questions'; 
        qNo++;

  if (qNo < questions.length) { 
    document.getElementById('Pic').src = questions[qNo][0]; 
        cnt++; 
  }else{ 
    alert('Quiz is done. You got ' + correct + ' points!'); 
  }
}

onload = function() {
    document.getElementById('Pic').src = questions[0][0];
}

</script>
</head>

<body>

<div align="center">
    <h1>Which car is it?</h1>
    <img src="" id="Pic" height="200" width="250">
    <p>Is it 
        <button onclick="NextQuestion('0')">Red</button>
        <button onclick="NextQuestion('1')">Black</button>
        <button onclick="NextQuestion('2')">Yellow</button> 
    <p>Your score: <br>
    <span id="score"></span>
</div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

好吧,你的问题在一个数组中。所以你应该研究的是如何随机化数组的顺序。

questions.sort(function() { return Math.floor(Math.random() * 2); });