测验应为正确答案给出 +2 分,为错误给出 -1 分,然后使用提交按钮和清除或重置按钮显示最终分数。
我需要将问题从文本字段更改为单选按钮(如果可能,请使用 getradiovalue() )。 (我添加了较早的一个,因为我认为这更难更正,我不确定如何继续使用标记和单选按钮)
var myVar;
function startTimer() {
myVar = setInterval(function () {
myTimer()
}, 1000);
timelimit = maxtimelimit;
}
function myTimer() {
if (timelimit > 0) {
curmin = Math.floor(timelimit / 60);
cursec = timelimit % 60;
if (curmin != 0) {
curtime = curmin + " minutes and " + cursec + " seconds left";
} else {
curtime = cursec + " seconds left";
}
$_('timeleft').innerHTML = curtime;
} else {
$_('timeleft').innerHTML = timelimit + ' - Out of Time - no credit given for answer ';
// clearInterval(myVar);
checkAnswer();
}
timelimit--;
}
var pos = 0,
posn, choice, correct = 0,
rscore = 0;
var maxtimelimit = 60,
timelimit = maxtimelimit;
var questions = [
['Chair', 'Table'],
['Pen', 'Pencil'],
['Box', 'Circle'],
['Triangle', 'Rectangle'],
['Gun', 'Knife']
];
var questionOrder = [];
var maxNumberOfQuestions = questions.length;
var maxNumberToDisplay = 5;
function setQuestionOrder() {
questionOrder.length = 0;
for (var i = 0; i < maxNumberOfQuestions; i++) {
questionOrder.push(i);
}
questionOrder.sort(randOrd);
pos = 0;
posn = questionOrder[pos];
}
function $_(IDS) {
return document.getElementById(IDS);
}
function randOrd() {
return (Math.round(Math.random()) - 0.5);
}
function renderResults() {
var test = $_("test");
test.innerHTML = "<h2>You got " + correct + " of " + maxNumberToDisplay + " questions correct < /h2>";
$_("test_status").innerHTML = "Test Completed";
$_('timeleft').innerHTML = '';
test.innerHTML += '<button onclick="location.reload()">Re-test</a> ';
setQuestionOrder();
correct = 0;
clearInterval(myVar);
return false;
}
function renderQuestion() {
var test = $_("test");
$_("test_status").innerHTML = "Question " + (pos + 1) + " of "+maxNumberToDisplay;
if (rscore != 0) {
$_("test_status").innerHTML += '<br>Currently: ' +
(correct / rscore * 100).toFixed(0) + '% correct';
}
var question = questions[posn][0];
var chA = questions[posn][1];
test.innerHTML = "<h3>" + question + "</h3>";
test.innerHTML += "<input type='text' id='choices' value=''><p>";
test.innerHTML += "<button onclick='checkAnswer()'>Submit Answer</button>";
}
function setupTimer() {
timelimit = maxtimelimit;
clearInterval(myVar);
startTimer();
}
function checkAnswer() {
var correctChoice = questions[posn][1];
var userChoice = document.getElementById("choices").value;
rscore++;
if (userChoice.toLowerCase() == correctChoice.toLowerCase() && timelimit >
0) {
correct++;
}
pos++;
posn = questionOrder[pos];
if (pos < maxNumberToDisplay) {
renderQuestion();
} else {
renderResults();
}
}
window.onload = function () {
setQuestionOrder();
renderQuestion();
setupTimer();
}
<div>
<h2 id="test_status"> Quiz</h2>
<h3 id="timeleft">Time left</h3>
</div>
<div id="test"></div>