我试图用javascript建立一个基本的数学游戏

时间:2013-03-22 14:13:08

标签: javascript

这是我的所有代码,我希望你能看到我出错的地方。

           var $randomnumber1;
            var $randomnumber2;
            var $answer;            
            var $counter = 1;
            var $data;
    function start_game() { 
         document.getElementById('counternumber').innerHTML = $counter; 
         document.getElementById('txtbox').value = "";                  
         $randomnumber1 = Math.floor(Math.random()*11);                 

            document.getElementById('num1').innerHTML = $randomnumber1; 
                $answer = $randomnumber1 + $randomnumber2;
                $counter++; 
                setFocus();                 
    }           
    function check_answer() {
            var $txt = document.getElementById('txtbox'); 
            var $value = $txt.value;
                if ($value == $answer) {
                    alert('You are correct');
                }
                else {
                    alert('You are incorrect, the answer was ' + $answer);
                }
            document.getElementById('txtbox').value = "";               
            document.getElementById('num1').innerHTML = "";
            document.getElementById('num2').innerHTML = ""; 
                     $randomnumber1 = Math.floor(Math.random()*11); 
                     $randomnumber2 = Math.floor(Math.random()*11);
                     document.getElementById('num1').innerHTML = $randomnumber1; 
                     document.getElementById('num2').innerHTML = $randomnumber2;

                        $answer = $randomnumber1 + $randomnumber2;
                document.getElementById('counternumber').innerHTML = $counter;
                $counter++; 
                    if ($counter > 4) {
                        alert ('End of game......Thanks for playing');
                        $counter = 1;
                            document.getElementById('num1').innerHTML = "";
                            document.getElementById('num2').innerHTML = ""; 
                    }

function addAnswers(){             var data = $ randomnumber1 +“+”+ $ randomnumber2 +“=”+($ randomnumber1 + $ randomnumber2)             var newListItem = document.createElement('li');             var newText = document.createTextNode(data);                     newListItem.appendChild(newText);                     的document.getElementById( “ANS”)的appendChild(newListItem)。         }

        <div class="container" id="wrapper">
        <h3>Games Played = <b id='counternumber'></b> / 3 </h3>
        <div id="total">Total: <p id="sumtotal"></p></div>

            <div class="left"><h3 id="num1"></h3></div>
            <div class="plus"><img src="images/plus.png"></div>
            <div class="right"><h3 id="num2"></h3></div>


        <button onclick="start_game(); myCountDown()">Start Game</button>

            <form id="form">
                 <input type="text" id="txtbox" /> 
                 <input type="button" value="Answer" onclick="check_answer(); setFocus(); ShowResults(); sumResults()" />
            </form> 
        </div>
    <a href="index.html">home page</a>

    </div> <!-- /content -->

    <div id="aside">

    </div>

1 个答案:

答案 0 :(得分:0)

你可以用这个:

        function addAnswers()
        {
            var data = $randomnumber1 + " + " + $randomnumber2 + " = " + ($randomnumber1+$randomnumber2)
            var newListItem = document.createElement('li');
            var newText = document.createTextNode(data);
            newListItem.appendChild(newText);
            document.getElementById("ans").appendChild(newListItem);
        }

您的问题尚不清楚,但这应该与您正在寻找的内容类似。您需要做的是添加一个ID为<ul>的{​​{1}},每次运行时,此函数都会向其附加ans个组件(您的答案)。答案本身将存储到li

试一试 - 如果您想要自定义帮助,请更新您的答案。