Javascript提示功能

时间:2017-09-20 22:42:06

标签: javascript

我正在构建一个猜谜游戏,其中包括生成一个数字和一个访问我页面的用户猜测它。 这个数字应该是1-5,你应该只有3次尝试猜测它。

到目前为止,我已经能够提示用户,并成功生成一个数字。然而,这真的是关于它。我的主要问题是:

  1. 让程序告诉用户这是不正确的猜测
  2. 成功显示尝试次数。

    <script type="text/javascript">
        var guesses = 3;//The number of guesses         
        alert ("I'm thinking of a number between 1 and 5. You've got 3 attempts to guess it");
        while ( guesses > 0 ) {
            var number = Math.random() * 5; //Generate a number between 1 and 5
            number = Math.ceil( number ); //Round it to 5
            var guess = prompt (" tries remaining. Go ahead!");
            guesses = guesses - 1;
            if (guess = number)
                alert ("You guessed the number!");
            else
                alert ("Unfortunate. Try again!");
        }
    </script>
    </head>
        <body>
            <script type="text/javascript">
                document.write ("The number was " + number);
            </script>
        </body>
    </html>
    
  3. 要为第二个问题添加更多内容,您可能已经注意到我有这行代码:

    var guess = prompt (" tries remaining. Go ahead!");
    

    然而,当我这样做时:

    var guess = prompt ( + guesses " tries remaining. Go ahead!");
    

    代码无法运行

    任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

我可以运行你的代码。我将它粘贴到下面的一个片段中 我看到的错误是我总是猜测正确的数字,不管我的猜测。

我甚至可以点击取消,它仍然认为我猜到了正确的号码。 此外,我从未看到我的尝试次数。 此外,即使我猜对了,你也会继续循环。

<script type="text/javascript">
    var guesses = 3;//The number of guesses         
    alert ("I'm thinking of a number between 1 and 5. You've got 3 attempts to guess it");
    while ( guesses > 0 ) {
        var number = Math.random() * 5; //Generate a number between 1 and 5
        number = Math.ceil( number ); //Round it to 5
        var guess = prompt (" tries remaining. Go ahead!");
        guesses = guesses - 1;
        if (guess = number)
            alert ("You guessed the number!");
        else
            alert ("Unfortunate. Try again!");
    }
</script>
</head>
    <body>
        <script type="text/javascript">
            document.write ("The number was " + number);
        </script>
    </body>
</html>

问题在于你的逻辑......我已经为你解决了这个问题 请参阅我在代码中的评论,但总结如下:

  • if语句中的表达错误
  • 每次猜测后分配一个新的随机数
  • 尝试剩余未添加到字符串(连接)

 <script type="text/javascript">
        var guesses = 3;//The number of guesses         
        // ##CHANGED## 
        // Pulling the number outside the loop - because otherwise you are generating 
        // a new random number after each guess.
        var number = Math.random() * 5; //Generate a number between 1 and 5
        number = Math.ceil( number ); //Round it to 5
        // ##
        alert ("I'm thinking of a number between 1 and 5. You've got 3 attempts to guess it");
        while ( guesses > 0 ) {
            //## CHANGED - concatenate guesses counter to prompt string   
            var guess = prompt (guesses + " tries remaining. Go ahead!");
            guesses = guesses - 1;
            
            //## CHANGED CONDITION INSIDE IF BELOW ##
            // Previously you were assigning a variable with =
            // == would do an implicit type conversion, which is what we want
            // thus typing in "2" as your string gets converted to the number 2
            
            if (guess == number) {
                alert ("You guessed the number!");
                guesses = 0; // ## HACKY BUT EASY WAY TO EXIT THE LOOP ONCE DONE
             }
            else {
                alert ("Unfortunate. Try again!");
            }
        }
    </script>
    </head>
        <body>
            <script type="text/javascript">
                document.write ("The number was " + number);
            </script>
        </body>
    </html>

答案 1 :(得分:-1)

你的第一个问题看起来是因为你没有做比较,而是做了一项任务。这将比较两个数字:

if (guess === number)

有关比较的更多信息以及您几乎总是使用===代替==的原因,请查看此处:https://stackoverflow.com/a/359509

现在,至于你的第二个问题,你是在猜测之前放置+而不是之后。将此视为“添加字符串”。这就是看起来的样子:

var guess = prompt (guesses + " tries remaining. Go ahead!");