简体   繁体   中英

How do I make a document wait for an assigned input value?

I am writing a number guessing game. Every time I run the document it automatically goes to my outer else statement and I know why, but I can't figure out how to make it wait for an input value of the textbox. I want to set "guess" = to the value of the text box by pressing submit which will then enter the if statements. At the moment it is automatically setting to null and causing the error. Here is the code.

<head>
        <title>Guessing Game</title>
</head>

<body>
        <h1>Number Guessing Game</h1><br>
        <button onclick = "search(1,100)">Press to play!</button>
</body>

<script type="text/javascript">

    var counter = 0;
    var randomNumber = Math.floor(Math.random()*100+1);
    function search(first, last){
                document.open();
                document.write("<h1>Number Guessing Game</h1><br>");
                document.write("<input id = 'guessBox' type = 'text'>&nbsp;");
                document.write("<input type = 'submit' id = 'submit' value = 'Submit'><br>");
                document.write("<h2>Numbers Left</h2>");
                for(var i = first; i <= last; i++){
                    document.write(i + " ");
                }
                document.write("<br><br><h3>Number of tries: " + counter + "</h3>");
                document.close();
                var guess = document.getElementById('guessBox').value;
                //var guess = prompt("Guess!");
                myguess = parseInt(guess);
        if(myguess <= last && myguess >= first && cont == true){
            if(myguess == randomNumber){
                counter++;
                if(counter <=3){
                alert("WOW, that was amazingly quick! You found it in " + counter + " tries.");
                }
                else if(counter < 6 && counter >= 4){
                    alert("Not bad! You found it in " + counter + " tries.");
                }
                else if(counter < 10 && counter >= 6){
                    alert("Ouch! You found it in " + counter + " tries.");
                }
                else{
                    alert("Having a bad day aren't you? You found it in "+ counter + " tries");
                }

            }
            else if(myguess < randomNumber){
                first = myguess+1;
                alert("Try again! The number is higher.");
                counter++;
                search(first, last);
            }
            else{
                last = myguess-1;
                alert("Try again! The number is lower.");
                counter++;
                search(first, last);        
            }
        }
        else{
            alert("Invalid Number, try again.");
            search(first, last);
        }
    }

</script>

Have you tried to disable the button, then add an onChange event to your text box so that you can enable the button once you have the desired input? You will need to add an ID or Name value to your button so it can be accessed? And to add what @LGSon added:

if(myguess <= last && myguess >= first && cont == true){, you check the variable cont, which I can't find in your code, so if it is not declared and set somewhere, you code will always take the outer route.

Second, you need to split your search function into 2 functions, one that generates the input, one that runs when someone entered a value (which can be fired on keypress or with a button)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM