1

簡単な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>

コードは私の大学から提供されました。ユーザーが取得したすべての正解をカウントする単純なカウンターと、カウンターをゼロにリセットするリセットボタンを保持したいですか?

これが本当に基本的なことのように聞こえたら申し訳ありませんが、私はJavascriptの経験がありません。

ありがとう。

4

2 に答える 2

2

私はテストしていませんが、コードはデフォルトでほとんど機能していると思いますが、唯一の問題はscore、グローバルスコープで変数を宣言していないことです。そうすれば、他の関数が同じ変数にアクセスできます。

var score = 0;ページの先頭に追加すると、機能するvarようになり、後でscore++;1つ増やしたり、score = 0;リセットしたりすることなく使用できます。残りのコードは、学校での作業なので、パズルとして残しておきます。グローバル変数の使用法について説明します。:)ただし、グローバル変数がないと生きていくのは難しいこともありますが、それは悪い習慣としても知られているため、グローバル変数をたくさん作成することは避けてください。グローバル変数、悪い習慣からもっと読む?

コードのデバッグのヒント:

var scoreElement=document.getElementById("score");

この行はDOMがロードされる前に実行されるため、nullになる可能性があります。行を変更してみてください。

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

document.getElementById("score").value=score;

また、すべてのJavaScriptをページの下部(たとえば、</body>タグの下)に移動します。これが正しい方法です。また、実際に使用していないのにjquery.jsをロードしているのはなぜですか?たとえば、すべてのjavascriptコードをドキュメント内に追加できます。このような:

$(function() {

    // your javascript code

});
于 2013-03-17T13:42:59.937 に答える
1

関数checkAdd(){}の外で、このように変数スコアを宣言する必要があります。

var score=0;

スコアを表示して申し訳ありませんが、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>
于 2013-03-17T13:06:08.407 に答える