Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript跟踪正确答案?_Javascript_Jquery_Html_Counter - Fatal编程技术网

如何使用javascript跟踪正确答案?

如何使用javascript跟踪正确答案?,javascript,jquery,html,counter,Javascript,Jquery,Html,Counter,我有一个简单的Javascript数学程序。它让你回答方程式,当它检查你的答案时,它会提醒你答案是正确的还是错误的 以下是添加代码: function randomAdd() { var x=document.getElementById("AddN1"); x.value=Math.floor((Math.random()*12)+1); var y=document.getElementById("Ad

我有一个简单的Javascript数学程序。它让你回答方程式,当它检查你的答案时,它会提醒你答案是正确的还是错误的

以下是添加代码:

function randomAdd()
        {
            var x=document.getElementById("AddN1");
            x.value=Math.floor((Math.random()*12)+1);

            var y=document.getElementById("AddN2");
            y.value=Math.floor((Math.random()*12)+1);
        }

        function checkAdd()
        {
            var z=document.getElementById("AddA2");
            z.value= parseInt(document.getElementById("AddN1").value) + parseInt(document.getElementById("AddN2").value);


            if(parseInt(document.getElementById("AddA1").value)==z.value)
            {
                score=score+1;
                alert("Addition: Correct");
            }
            else
            {
                alert("Addition: Incorrect");
            }
        }
<form name="Addition">
        <table>
            <tr>
                <th>Addition</th> <th></th> <th></th> <th></th> <th></th> <th></th>
            </tr>
            <tr>
                <th>Number 1</th> <th></th> <th>Number 2</th> <th></th> <th>Type Your Answer</th> <th>Correct Answer</th>
            </tr>
            <tr>
                <td><input type="text" name="AddN1" id="AddN1"></td> <td>+</td> <td><input type="text" name="AddN2" id="AddN2"></td> <td>=</td> <td><input type="text" name="AddA1" id="AddA1"></td> <td><input type="text" name="AddA2" id="AddA2"></td>
            </tr>
            <tr>
                <td><input type="button" value="Get Numbers" onclick="randomAdd();"> <td></td> <td></td> <td></td> <td><input type="button" value="Check Answer" onclick="checkAdd();"> <th></th>
            </tr>
        </table>
</form>
函数随机添加()
{
var x=document.getElementById(“AddN1”);
x、 值=数学地板((数学随机()*12)+1);
var y=document.getElementById(“AddN2”);
y、 值=数学地板((数学随机()*12)+1);
}
函数checkAdd()
{
var z=document.getElementById(“AddA2”);
z、 value=parseInt(document.getElementById(“AddN1”).value)+parseInt(document.getElementById(“AddN2”).value);
if(parseInt(document.getElementById(“AddA1”).value)==z.value)
{
分数=分数+1;
警告(“添加:正确”);
}
其他的
{
警告(“添加:不正确”);
}
}
附加
数字1数字2键入你的答案正确答案
+  =  
代码是我的大学提供给我的。 我想保留一个简单的计数器,可以计算用户得到的所有正确答案,还有一个重置按钮,可以将计数器重置回零

很抱歉,如果这听起来很基本,但我没有Javascript的经验


谢谢。

您只需要在函数checkAdd(){}之外声明如下的变量分数

然后,在您可以显示分数后,它会提醒您是否正确 对不起,现在我添加了resetScore函数

<form name="Addition">
        <table>
            <tr>
                <th>Addition</th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
            </tr>
            <tr>
                <th>Number 1</th>
                <th></th>
                <th>Number 2</th>
                <th></th>
                <th>Type Your Answer</th>
                <th>Correct Answer</th>
                <th>SCORE</th>
            </tr>
            <tr>
                <td>
                    <input type="text" name="AddN1" id="AddN1">
                </td>
                <td>+</td>
                <td>
                    <input type="text" name="AddN2" id="AddN2">
                </td>
                <td>=</td>
                <td>
                    <input type="text" name="AddA1" id="AddA1">
                </td>
                <td>
                    <input type="text" name="AddA2" id="AddA2">
                </td>
                <td>
                    <input type="text" name="score" id="score" value="0" readonly>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="button" value="Get Numbers" onclick="randomAdd();">
                <td></td>
                <td></td>
                <td></td>
                <td>
                    <input type="button" value="Check Answer" onclick="checkAdd();">
                </td>
                <td></td>
                <td>
                    <input type="button" value="Reset Score" onclick="resetScore();">
                </td>
            </tr>
        </table>
</form>
<script type="text/javascript">
var score=0;
function resetScore()
{   
    score=0;
    document.getElementById("score").value=score;
}
function randomAdd()
        {
            var x=document.getElementById("AddN1");
            x.value=Math.floor((Math.random()*12)+1);

            var y=document.getElementById("AddN2");
            y.value=Math.floor((Math.random()*12)+1);
        }


        function checkAdd()
        {
            var z=document.getElementById("AddA2");
            z.value= parseInt(document.getElementById("AddN1").value) + parseInt(document.getElementById("AddN2").value);

            if(parseInt(document.getElementById("AddA1").value)==z.value)
            {
                //score=score+1;
                score++;
                alert("Addition: Correct");
                document.getElementById("score").value=score;
            }
            else
            {   
                //score=score-1;
                score--;
                alert("Addition: Incorrect");
                document.getElementById("score").value=score;
            }
        }

</script>

附加
第一
二号
键入你的答案
正确答案
分数
+
=
var得分=0;
函数resetScore()
{   
得分=0;
document.getElementById(“分数”).value=分数;
}
函数randomAdd()
{
var x=document.getElementById(“AddN1”);
x、 值=数学地板((数学随机()*12)+1);
var y=document.getElementById(“AddN2”);
y、 值=数学地板((数学随机()*12)+1);
}
函数checkAdd()
{
var z=document.getElementById(“AddA2”);
z、 value=parseInt(document.getElementById(“AddN1”).value)+parseInt(document.getElementById(“AddN2”).value);
if(parseInt(document.getElementById(“AddA1”).value)==z.value)
{
//分数=分数+1;
分数++;
警告(“添加:正确”);
document.getElementById(“分数”).value=分数;
}
其他的
{   
//得分=1分;
分数--;
警告(“添加:不正确”);
document.getElementById(“分数”).value=分数;
}
}

我还没有测试,但我怀疑您的代码在默认情况下几乎可以正常工作,但唯一的问题是您没有在全局范围内声明
score
变量,这样其他函数就可以访问相同的变量,而无需进一步传递

添加
var得分=0应该可以使它工作,然后在没有
var
的情况下使用它,比如
score++增加1,
得分=0重置它,依此类推。。剩下的代码我留给你们的是一个谜团,因为它是学校的工作——只是想让你们了解全局变量的用法。:)然而,尽量避免创建大量全局变量,因为这也被称为坏习惯,尽管没有它们有时很难生活。阅读更多

针对代码的一些调试提示:

var scoreElement=document.getElementById("score");
这一行是在加载DOM之前执行的,因此它可能为null。。尝试更改行:

alert("Addition: Correct");
scoreElement.value=score;

同时将所有javascript移到页面底部(例如:
标记下)——这是正确的方法。还有,为什么在没有真正使用jquery.js的情况下加载它?例如,您可以将所有javascript代码添加到准备好的内部文档中。像这样:

$(function() {

    // your javascript code

});

谢谢你的回复,你能再详细一点吗?当你使用分数作为int时,为什么要将分数声明为string(分数=“”)?出于某种原因,分数不超过1?@Mauno,对不起,你是对的!那只是一开始,在我忘记之后,我会改变它。这是正确的功能。但是谢谢你@crm,还是一点都不算吗?您添加的内容对吗?啊,我最初打算尝试只使用Jquery,但决定不使用Jquery,我将删除该Jquery。请问您是否知道如何阻止计时器在页面加载时自动倒计时?很抱歉,这不相关。好的,您自己在第84行启动它,请参阅:var counter=setInterval(timer,1000);使var计数=60;和var计数器;作为其声明的分数以下的全局变量。然后在timer()或其他方法中启动它,counter=setInterval(timer,1000);并在需要时清除它。例如,如果count为60,则再次调用timer(),当其为0时,则停止它
document.getElementById("score").value=score;
$(function() {

    // your javascript code

});