Javascript猜猜游戏

时间:2012-08-04 05:29:34

标签: javascript

我是javascript和html的新手,所以我希望有人可以查看我的代码并告诉我为什么某些语法不起作用。在下面的代码中,我使用document.getElementById来检索我每次都要使用的元素。我确实将它们声明为gameTime()中的局部变量,并在整个函数中使用它。

    var randNum = Math.floor( 100*Math.random() ) +1;
    var numGuesses = 0;

    function gameTime()
    {   
        var guess = document.getElementById("guess").value;
        var status = document.getElementById("status"); 

        numGuesses++;

        if(guess == "")
        {
            alert("You did not guess a number!")
            numGuesses--;
        }
        else if(guess == randNum)
            status.value += (guess + " was the right number! It only took you " + numGuesses + " tries" + "\r")
        else if(guess > randNum)
            status.value += (guess + " is too high!" + "\r")
        else    
            status.value += (guess + " is too low!" + "\r")

        document.getElementById("guess").value = "";
    }

    function reset()
    {
        randNum = Math.floor( 100*Math.random() ) +1;
        numGuesses = 0;
        document.getElementById("guess").value = "";
        document.getElementById("status").value = "";
    }

    function quit()
    {
        document.getElementById("status").value += ("The correct number was " + randNum + "!\r")
    }

然后我通过全局声明这两个元素来尝试它,并在每个看起来都不起作用的函数中使用它,我只是不明白为什么。

    var randNum = Math.floor( 100*Math.random() ) +1;
    var numGuesses = 0;
    var guess = document.getElementById("guess").value;
    var status = document.getElementById("status");

    function gameTime()
    {   
        numGuesses++;

        if(guess == "")
        {
            alert("You did not guess a number!")
            numGuesses--;
        }
        else if(guess == randNum)
            status.value += (guess + " was the right number! It only took you " + numGuesses + " tries" + "\r")
        else if(guess > randNum)
            status.value += (guess + " is too high!" + "\r")
        else    
            status.value += (guess + " is too low!" + "\r")

        guess.value = "";
    }

    function reset()
    {
        randNum = Math.floor( 100*Math.random() ) +1;
        numGuesses = 0;
        guess.value = "";
        status.value = "";
    }

    function quit()
    {
        status.value += ("The correct number was " + randNum + "!\r")
    }

这里是这些js文件附带的html文件guess.js是第一个代码块,guessAgain.js是第二个代码块。

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Guessing Game</title>
            <script type="text/javascript" src="guessAgain.js"></script>
        </head>
        <body>
            <label for = "guess"> Your Guess: </label>
            <input type = "text" id = "guess" value = "" />    
            <input type = "button" onclick = "gameTime()" value = "Submit" />  
            <input type = "button" onclick = "reset()" value = "New Game" />
            <input type = "button" onclick = "quit()" value = "Quit" />
            <br>    
            <textarea id = "status" rows = "10" cols = "52"></textarea>  
        </body> 
    </html>

感谢您给我的任何见解。

2 个答案:

答案 0 :(得分:1)

您将猜测设置为element.value

应该是

var guess = document.getElementById("guess");

问题是你在这里使用guess作为元素:

guess.value = "";

您还需要将使用guess字符串的地方修改为guess.value

答案 1 :(得分:1)

问题1: 在主体加载之前调用以下内容,因此它们返回undefined。

var guess = document.getElementById("guess");
var status = document.getElementById("status");

只有在onload()事件加载正文后才能执行这些操作。

问题2:

此外,当您将值本身分配给变量时,它不会像文本框那样更新。 var guess = document.getElementById("guess").value,如果更改文本框,则guess的值不会更新。

如果你引用var guess = document.getElementById("guess"),那么,反复拨打电话 guess.value将返回输入字段的最新值。

<强> (Working Example)


不必要的复杂以下没有错。

        numGuesses++;   
        if(guess.value == "")
        {
            alert("You did not guess a number!")
            numGuesses--;
        }
        ...

相反,一旦被认为是有效的猜测,你可以增加numGuesses,即最后。