Javascript Guessing Game
<p>I'm brand new to javascript and html so I was hoping someone could peek at my code and tell me why certain syntax isn't working. In the code below I use document.getElementById to retreive the element I'd like to work with each time. I do declare them as local variables within gameTime() and use it throughout the function.</p>

<pre><code>    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 &gt; 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")
    }
</code></pre>

<p>Then I tried it by globally declaring the two elements and use it in every function which does not seem to work at all and I just don't understand why.</p>

<pre><code>    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 &gt; 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")
    }
</code></pre>

<p>And here's the html file that accompanies these js files guess.js is the first code block, guessAgain.js is the second code block.</p>

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

<p>Thanks for any insight you can give me.</p>
