简体   繁体   English

用循环替换条件语句

[英]Replacing a conditional statement with a loop

I am trying to make a guessing game that generates a random number which the user guesses. 我正在尝试制作一个猜测游戏,该游戏生成用户猜测的随机数。 This guessing game must use some kind of loop. 这个猜谜游戏必须使用某种循环。 Right now it is enclosed in a function that is called when the user clicks a button, and a boolean that limits the number of guesses the user can make. 现在,它包含在一个函数中,该函数在用户单击按钮时被调用,并且一个布尔值限制了用户可以做出的猜测的数量。

To my understanding, a loop runs without stopping until its conditions have been satisfied. 据我了解,循环一直运行直到满足其条件为止。 If I were to have a loop in this situation, the final output would be the loop's last iteration. 如果在这种情况下要进行循环,则最终输出将是循环的最后一次迭代。 NumGuesses would have a value of 6 because that was when the loop stopped. NumGuesses的值为6,因为那是循环停止的时间。 How can I replace the conditional with a loop and still have it work? 如何用循环替换条件语句并使其仍然起作用?

//Declare variables and constants 
var guessesLeft;                               //number of guesses the user has left
var userGuess;                                //user's guess
var highLimit = 1000;                         //high parameter
var numGuesses = 1;                           //number of guesess the user has guessed
var allowedGuesses = 6;                      //number of allowed guesses
var NL = "\n";                               //new line

    //function to be called when the user presses Submit Guess
    function guessingGame() { 
        //random number that is generated
        var randNum = Math.floor(Math.random() * highLimit + 1); 
        if (numGuesses <= allowedGuesses) { 
        userGuess = prompt("Enter a number between 1 and " + highLimit + ": " , "")
        userGuess = parseInt(userGuess);
        guessesLeft = (allowedGuesses - numGuesses) + " guesses left.";
            //If userGuess is in the specified parameters
            //If userGuess is larger than 1 and smaller than the higher parameter
            if ((userGuess > 1) && (userGuess < highLimit)) {

                //If userGuess does not equal randNum
                if (userGuess !== randNum) {

                    //If userGuess is too low
                    if (userGuess < randNum) {
                        //output 
                        document.numForm.gameResults.value = "You guessed too low! Please guess again. " + NL + "Your current number of guesses: " + numGuesses + NL + "You have " + guessesLeft;
                    }
                    //If userGuess is too high
                    else if (userGuess > randNum) {
                        document.numForm.gameResults.value = "You guessed too high! Please guess again. " + NL + "Your current number of guesses: " + numGuesses + NL + "You have " + guessesLeft;
                    }//end if                      
                }
                else {
                    //If userGuess is correct
                    document.numForm.gameResults.value = "You guessed correctly!";
                }//end if
            }

            else {
                document.numForm.gameResults.value =  "Your guess was out of the specified parameters. Please try again. " + NL + "Your current number of guesses: " + numGuesses + NL + "You currently have " + guessesLeft;
                    //user may have entered their guess wrong
                    numGuesses--;
            }//end if 
            //add to number of guesses so the program stops when it reaches the allowed number
            numGuesses++;
        }//end outer if statement
        return false;
    }//end guessingGame()

It sounds like the problem statement expects you to block waiting for user input as opposed to triggering a function in response to a DOM event. 听起来问题问题语句希望您阻止等待用户输入,而不是响应DOM事件而触发函数。

Have a look at window.prompt 看看window.prompt

Placing this in a loop would block the loop until the user responded. 将其放置在循环中将阻塞循环,直到用户做出响应为止。

Using prompt() as suggested by retrohacker and a do {} while () loop, the following code demonstrates this in action: 使用Retrohacker的 prompt()do {} while ()循环,下面的代码演示了这一点:

 var minNumber = 1; var maxNumber = 2; var guessesLeft = 3; var numberToBeGuessed = minNumber + (Math.random() * (maxNumber - minNumber + 1) | 0); var succeeded; var message = 'Make a guess'; do { if (prompt(message) == numberToBeGuessed) { alert('Correct, the number is ' + numberToBeGuessed); succeeded = true; } else { guessesLeft--; message = 'Try again, ' + guessesLeft + ' guess' + (guessesLeft > 1 ? 'es' : '') + ' left'; } } while (!succeeded && guessesLeft) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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