It is clear that I need to learn javascript. I am trying to write a test that will work offline so I can’t use PHP and I am using javascript instead. Right now I have a test that will give me the score for number of correct answers. The way I did it was to just add up the values where correct is 1 and incorrect is 0. So, I can get the number of correct answers. However, what I really want is to calculate the Kappa score. To do that I need to know the count for each group in this table:
True
Y N
Tester Y 11 01
N 10 00
here is my quiz:
test
<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>
In
getRBValue, you loop through the group elements, of which there are always two. The first one is always the Present choice and the second is always the Absent choice. In your current code, you can distinguish these by the value of the loop variableb. However, I would rewrite your code as:You could accumulate the raw counts in a 2D array, but in my opinion using explicit variables makes for more readable (if more verbose) code.