0

很明显,我需要学习 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>
4

1 回答 1

0

getRBValue中,您循环遍历组元素,其中总是有两个。第一个始终是 Present 选择,第二个始终是 Absent 选择。在您当前的代码中,您可以通过循环变量的值来区分它们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 数组中累积原始计数,但在我看来,使用显式变量会使代码更具可读性(如果更冗长)。

于 2012-11-07T14:09:18.023 回答