很明显,我需要学习javascript。我正在尝试编写一个可以脱机工作的测试,所以我不能使用PHP,而是使用javascript代替。现在我有一个测试,可以给我正确答案数的分数。我这样做的方法是只将正确值为1且不正确值为0的值相加。因此,我可以得到正确答案的数量。但是,我真正想要的是计算Kappa分数。为此,我需要知道此表中每个组的计数:
True
Y N
Tester Y 11 01
N 10 00
这是我的测验: 测试
<script type="text/javascript">
function getRBValue(group) {
//loop
for ( var b = 0; b < group.length; ++b ) {
if ( group[b].checked )
return Number(group[b].value);
}
return 0; }
function Score(form) {
var total = 0;
for ( var q = 1; q <= 10; ++q ) {
total += getRBValue( form["q"+q] );
}
form.score.value = total;
alert("You got " + total + " questions right!");
}
</script>
</head>
<body>
<h1>test</h1>
<p>The following exam will allow you to evaluate your ability to grade Trachoma.</p>
</body>
<form name="prequiz" onsubmit="Score(this); return false;">
<table cellpadding=15px border="0" width="65%" summary= "Questions and options are organized in a table.">
<tbody>
<tr> <td>
<b><img src="pics/1.jpeg"</b><br/>
<br/><input type="radio" name="q1" value="0" > Present<br/>
<br/><input type="radio" name="q1" value="1" > Absent<br/>
</td> </tr>
<tr>
<td>
<b><img src="pics/2.jpeg"</b><br/>
<br/> <input type="radio" name="q2" value="0" > Present<br/>
<br/> <input type="radio" name="q2" value="1" > Absent<br/>
</td> </tr>
<tr>
<td>
<b><img src="pics/3.jpeg"</b><br/>
<br/> <input type="radio" name="q3" value="1" > Present<br/>
<br/> <input type="radio" name="q3" value="0" > Absent<br/>
</td> </tr>
<tr>
<td>
<b><img src="pics/4.jpeg"</b><br/>
<br/> <input type="radio" name="q4" value="0" > Present<br/>
<br/> <input type="radio" name="q4" value="1" > Absent<br/>
</td> </tr>
<tr>
<td>
<b><img src="pics/5.jpeg"</b><br/>
<br/> <input type="radio" name="q5" value="0" > Present<br/>
<br/> <input type="radio" name="q5" value="1" > Absent<br/>
</td> </tr>
<tr>
<td>
<b><img src="pics/6.jpeg"</b><br/>
<br/> <input type="radio" name="q6" value="0" > Present<br/>
<br/> <input type="radio" name="q6" value="1" > Absent<br/>
</td> </tr>
<tr>
<td>
<b><img src="pics/7.jpeg"</b><br/>
<br/> <input type="radio" name="q7" value="0" > Present<br/>
<br/> <input type="radio" name="q7" value="1" > Absent<br/>
</td> </tr>
<tr>
<td>
<b><img src="pics/8.jpeg"</b><br/>
<br/> <input type="radio" name="q8" value="1" > Present<br/>
<br/> <input type="radio" name="q8" value="0" > Absent<br/>
</td> </tr>
<tr>
<td>
<b><img src="pics/9.jpeg"</b><br/>
<br/> <input type="radio" name="q9" value="1" > Present<br/>
<br/> <input type="radio" name="q9" value="0" > Absent<br/>
</td> </tr>
<tr>
<td>
<b><img src="pics/10.jpeg"</b><br/>
<br/> <input type="radio" name="q10" value="0" > Present<br/>
<br/> <input type="radio" name="q10" value="1" > Absent<br/>
</td> </tr>
</tbody> </table> </center>
<br><br>
<input type="submit" name="submit" value="Score exam now">
<br/><br/>
<b>Your score:</b><input type="text" name="score" readonly="readonly" />
<br/>
</form> </html>
答案 0 :(得分:0)
在getRBValue
中,循环遍历组元素,其中总共有两个元素。第一个始终是当前选择,第二个始终是缺席选择。在当前代码中,您可以通过循环变量b
的值来区分它们。但是,我会将您的代码重写为:
var PP, // correct is Present; user answered Present
PA, // correct is Present; user answered Absent
PN, // correct is Present; user did not answer
AP, // correct is Absent; user answered Present
AA, // correct is Absent; user answered Absent
AN; // correct is Absent; user did not answer
function ScoreGroup(group) {
var presentIsCorrect = Number(group[0].value);
if (group[0].checked) {
// user answered Present
if (presentIsCorrect) {
PP++;
} else {
AP++;
}
} else if (group[1].checked) {
// user answered Absent
if (presentIsCorrect) {
PA++;
} else {
AA++;
}
} else {
// user did not answer the question
if (presentIsCorrect) {
PN++;
} else {
AN++;
}
}
}
function Score(form) {
PP = PA = PN = AP = AA = AN = 0;
for ( var q = 1; q <= 10; ++q ) {
ScoreGroup( form["q"+q] );
}
var totalCorrect = PP + AA;
// TODO: compute kappa from PP, PA, PN, AP, AA, AN, and totalCorrect
form.score.value = totalCorrect;
alert("You got " + totalCorrect + " questions right!");
}
您可以在2D数组中累积原始计数,但在我看来,使用显式变量会使代码更具可读性(如果更详细)。