用户使用函数猜测1-1000的数字

时间:2017-10-11 03:17:24

标签: javascript html function

我正在尝试制作一个程序,提示用户猜测1到1000之间的数字。程序生成一个随机数,然后用户必须猜测数字,直到他们正确为止。如果他们的猜测太低或太高,程序会提醒用户。输入正确的号码后,他们会被祝贺,并询问他们是否想再次运行它。我已经阅读了我的书,甚至在网上寻求指导,但是尽管我付出了最大努力,它还是显示带有计算按钮的文本字段......没有窗口消息或任何东西。请帮忙,因为我很难过。这就是我到目前为止所做的:

    <!DOCTYPE html>
    <html>
            <head>
                    <meta charset="utf-8">
                    <title>Assignment 9.25</title>

                <script type="text/javascript">
                    var inputField;
                    var guess;
                    var calculateButton;

                    function startGame() {
                    window.alert("Guess a number between 1 and 1000 in the text field.");
                    calculateButton = document.getElementById("calculate");
                    //calculateButton.disable = true;
                    guessNum();
                        }

                        function randomNum(random) {
                            return Math.floor(1 + (Math.random() * 999));
                        }

                        function guessNum() {
                            inputField = document.getElementById("entry");
                            guess = parseFloat(inputField.value);

                            while (randomNum(random) != guess) {
                                if (randomNum(random) > guess) {
                                    window.alert("Too low. Try again.");
                                }
                                else if (randomNum(random) < guess) {
                                    window.alert("Too high. Try again.");
                                }
                            }

                            window.alert("Congratulations. You guessed the number!");
                            playAgain();
                        }

                        function playAgain() {
                            var again = window.prompt("Enter 'yes' to play again");
                                if (again == "yes") {
                                    Start();
                                    calculateButton.disabled = false;
                                else if (again == "no") {
                                            alert ("Thank you for playing! Goodbye!")
                                    calculateButton.disabled = true;
                                }
                        }

                        function Start() {
                            var calculateButton = document.getElementById("calculate");
                            calculateButton.addEventListener( "click", startGame, false );
                        }

                    window.addEventListener("load", Start, false);
                </script>
            </head>
            <body>
                <form action="#">
                    <div>
                        <label>Your guess here:
                            <input id="entry" type="number">
                        </label>
                        <br>
                        <input id="calculate" type="button" value="Calculate">
                    </div>
                </form>
            </body>
    </html>

2 个答案:

答案 0 :(得分:1)

第45行缺少 }

    <!DOCTYPE html>
    <html>
            <head>
                    <meta charset="utf-8">
                    <title>Assignment 9.25</title>

                <script type="text/javascript">
                    var inputField;
                    var guess;
                    var calculateButton;

                    function startGame() {
                    window.alert("Guess a number between 1 and 1000 in the text field.");
                    calculateButton = document.getElementById("calculate");
                    //calculateButton.disable = true;
                    guessNum();
                        }

                        function randomNum(random) {
                            return Math.floor(1 + (Math.random() * 999));
                        }

                        function guessNum() {
                            inputField = document.getElementById("entry");
                            guess = parseFloat(inputField.value);

                            while (randomNum(random) != guess) {
                                if (randomNum(random) > guess) {
                                    window.alert("Too low. Try again.");
                                }
                                else if (randomNum(random) < guess) {
                                    window.alert("Too high. Try again.");
                                }
                            }

                            window.alert("Congratulations. You guessed the number!");
                            playAgain();
                        }

                        function playAgain() {
                            var again = window.prompt("Enter 'yes' to play again");
                                if (again == "yes") {
                                    Start();
                                    calculateButton.disabled = false;
                                }
                                else if (again == "no") {
                                            alert ("Thank you for playing! Goodbye!")
                                    calculateButton.disabled = true;
                                }
                        }

                        function Start() {
                            var calculateButton = document.getElementById("calculate");
                            calculateButton.addEventListener( "click", startGame, false );
                        }

                    window.addEventListener("load", Start, false);
                </script>
            </head>
            <body>
                <form action="#">
                    <div>
                        <label>Your guess here:
                            <input id="entry" type="number">
                        </label>
                        <br>
                        <input id="calculate" type="button" value="Calculate">
                    </div>
                </form>
            </body>
    </html>

答案 1 :(得分:0)

如果你介意的话

Jquery way

var a = Math.floor((Math.random() * 100) + 1);
$(document).ready(function(){
  $('#numberrandom').text(a);
  });
  $('#calculate').bind('click',function(){
    entrynumber=$('#entry').val();
  if(entrynumber > a){
  alert('Your number is higher than it')
  }
  else if(entrynumber < a){
  alert('Your number is lower than it')
  }
  else if(entrynumber == a){
  alert('nice you won')
  location.reload();
  }
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <body>
                <form action="#">
                    <div>
                        <label>Your guess here:
                            <input id="entry" type="number">
                        </label>
                        <br>
                        <input id="calculate" type="button" value="Calculate">
                        <span>generated number: </span><span id='numberrandom'></span>
                    </div>
                </form>
            </body>