【问题标题】:Getting wrong total in javascript在javascript中得到错误的总数
【发布时间】:2016-08-08 04:36:08
【问题描述】:

我正在尝试使用 html 和 JavaScript 准备在线考试页面,您可以在其中看到以下代码: 我有两个问题: 问题1: 我想向这个在线考试页面显示计时器,代码如下: a) 考试应该在固定的时间完成。如果考官不能及时完成考试,那么他应该收到关于时间到的警报消息,并且必须显示问题 2 代码的总页数的最后一页这个定时器代码的下面。

Timer Code:
var tim;
       var showscore=Math.round(correct/questions.length*100);
        var min = 1;
        var sec = 60;
        var f = new Date();
        function starttime() {
            showtime();
            document.getElementById("starttime").innerHTML = "<h4>You started your Exam at " + f.getHours() + ":" + f.getMinutes()+"</h4>"; 
        }
        function showtime() {
            if (parseInt(sec) > 0) {
                sec = parseInt(sec) - 1;
                document.getElementById("showtime").innerHTML = "Your Left Time is :"+min+" Minutes :" + sec+" Seconds";
                tim = setTimeout("showtime()", 1000);
            }
            else {
                if (parseInt(sec) == 0) {
                    min = parseInt(min) - 1;
            document.getElementById("showtime").innerHTML = "Your Left Time is :"+min+" Minutes :" + sec+" Seconds";
                    if (parseInt(min) == 0) {
                        clearTimeout(tim);
            alert("Time Up");


            /*_("test_status").innerHTML = "Test Completed";
            test.innerHTML = "<h2>You got "+correct+" of "+questions.length+" questions correct</h2>";
            test.innerHTML = "<h2>You got "+showscore +"% out of "+questions.length+"</h2>";
            test.innerHTML = "<button onclick='EndExam()'>End the Exam</button>";
            pos = 0;
            correct = 0;
            clearTimeout(tim);
            document.getElementById("endtime").innerHTML = "You Finished exam at Time is :"+min+" Minutes :" + sec+" Seconds";
            document.getElementById("starttime").style.display += 'none';
            document.getElementById("showtime").style.display += 'none';
            //document.getElementById("showtime").style.display += 'block';
            return false;*/

                        window.location.href = "Loginpage.htm";
                    }
                    else {
                        sec = 60;
                        document.getElementById("showtime").innerHTML = "Your Left Time is :" + min + " Minutes :" + sec + " Seconds";
                        tim = setTimeout("showtime()", 1000);
                    }
                }

            }
        }

问题 2: 但是计数存在问题,当我尝试回答 2 个问题并留下接下来的 2 个问题而不标记或选择单选选项时,总共 4 个问题会显示错误的总数。

**wrong answer :You got 3 of 4 questions correct
correct answer: You got 2 of 4 questions correct**


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
div#test{ border:#000 1px solid; padding:10px 40px 40px 40px; }
</style>
</head>
<body>
<h2 id="test_status"></h2>
<div id="test"></div>
<script>
var pos = 0, test, test_status, questions, choice, choices, chA, chB, chC, correct = 0;
var questions = [
    [ "What is 10 + 4?", "12", "14", "16", "B" ],
    [ "What is 20 - 9?", "7", "13", "11", "C" ],
    [ "What is 7 x 3?", "21", "24", "25", "A" ],
    [ "What is 8 / 2?", "10", "2", "4", "C" ]
];
function _(x){
    return document.getElementById(x);
}
function renderQuestion(){
    test = _("test");
    if(pos >= questions.length){
        test.innerHTML = "<h2>You got "+correct+" of "+questions.length+" questions correct</h2>";
        _("test_status").innerHTML = "Test Completed";
        pos = 0;
        correct = 0;
        return false;
    }
    _("test_status").innerHTML = "Question "+(pos+1)+" of "+questions.length;
    question = questions[pos][0];
    chA = questions[pos][1];
    chB = questions[pos][2];
    chC = questions[pos][3];
    test.innerHTML = "<h3>"+question+"</h3>";
    test.innerHTML += "<input type='radio' name='choices' value='A'> "+chA+"<br>";
    test.innerHTML += "<input type='radio' name='choices' value='B'> "+chB+"<br>";
    test.innerHTML += "<input type='radio' name='choices' value='C'> "+chC+"<br><br>";
    test.innerHTML += "<button onclick='checkAnswer()'>Submit Answer</button>";
}
function checkAnswer(){
    choices = document.getElementsByName("choices");
    for(var i=0; i<choices.length; i++){
        if(choices[i].checked){
            choice = choices[i].value;
        }
    }
    if(choice == questions[pos][4]){
        correct++;
    }
    pos++;
    renderQuestion();
}
window.addEventListener("load", renderQuestion, false);
</script>

</body>
</html>

谁能帮忙解决定时器的问题?

【问题讨论】:

  • 你问的太多了,不好回答!!!
  • 一切都在客户端上?
  • 猜测:您可能需要检查 checkAnswer() 中的 pos 值...
  • 如你所说,我一直保留“choice=-1”。谢谢。

标签: javascript c# css html


【解决方案1】:

这回答了两个问题之一。我声称在问题 2 未重置后的答案选择,因此它也因问题 4 的答案而获得荣誉。 这应该可以解决它:

function checkAnswer(){
    choices = document.getElementsByName("choices");
    choice='Z'; //here is the new code, avoid matches based on earlier
    for(var i=0; i<choices.length; i++){
        if(choices[i].checked){
            choice = choices[i].value;
        }
    }
    if(choice == questions[pos][4]){
        correct++;
    }
    pos++;
    renderQuestion();
}

【讨论】:

  • 感谢第二个问题的帮助
  • 你能帮助我在问题1中提到的如何保持在线考试的计时器
  • 很抱歉,但我不确定自己的情况如何。这需要团队合作。
  • 说这是一个 10 分钟的测试,你想给出一分钟的警告。当他们开始测试时说类似 var c=setTimeout(function()(_("test status").innerHTML+="

    还剩一分钟

    ",9*60000); 以及您在哪里设置“测试Completed" call clearTimeout(c); 如果你想每分钟更新一次剩余时间,比如说,还要查看 setInterval(和 clearInterval)。
  • 是的,我现在几乎可以肯定你想要 SetInterval,而不是 SetTime,ShowTime 必须再次设置时间(所以你会摆脱它)。只需 tim=SetInterval("ShowTime"(), 1000) 并将 ClearTimeout(tim) 更改为 ClearInterval(tim)。至少,如果需要,跟踪和跟踪以进行调试会更简单。如果你能描述不良行为,那就太好了。同样在 min = parseInt(min) - 1; 之后我认为您需要将 sec 设置为 59。
猜你喜欢
  • 1970-01-01
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
  • 2014-08-02
  • 1970-01-01
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
相关资源
最近更新 更多