将循环插入Javascript数字猜谜游戏

时间:2017-05-06 23:49:41

标签: javascript loops

对于作业,我需要制作一个JS数字猜谜游戏。它需要包含一个循环来检查用户的猜测和重置游戏按钮。我的问题是让循环工作。我希望转数从10开始。每次用户做出错误的猜测时,它们的转数减少1,如果他们猜对了,它们的转数是0.如果他们按下"开始新游戏"按钮,应生成一个新数字,并将转数重置为10.

循环并不特别需要是一个while循环,我只需要代码中的一个用于我的赋值。有人可以帮帮我吗?

<body>
    <!-- GAME INSTRUCTIONS -->
    <h1>Number Guessing Game</h1>
    <p>A random number between 1 and 100 has been generated. Can you guess it?</p>
    <!-- FORM (Includes button to confirm guess, input box, and output box) -->
    <form id="Input" name="Input">
        <input name="guess" placeholder="Insert your guess" type="number">
        <input name="requestInfo" onclick="getResults()" type="button" value="Confirm">
        <p></p>
        <textarea cols="50" name="results" readonly="true" rows="8"></textarea>
        <p></p><input name="newGame" onclick="resetGame()" type="button" value="Start New Game">
    </form><!-- JAVASCRIPT START -->
    <script type="text/javascript">

// Define variables
var num = Math.floor(Math.random() * 100) + 1;
var turns = 10;

function checkNumber() {
    var guess = parseFloat(document.Input.guess.value);
    while (turns > 0) {
            if (guess == num) {
                turns = 0;
                document.Input.results.value = "Congratulations, you won! The mystery number was " + num + ".";
            } else if (guess < num) {
                turns--;
                document.Input.results.value = "Your guess was too low. Turns remaining: " + turns;
            } else if (guess > num) {
                turns--;
                document.Input.results.value = "Your guess was too high. Turns remaining: " + turns;
            }
    }
}

function resetGame() {
    turns = 10;
    num = Math.floor(Math.random() * 100) + 1;
    document.Input.guess.value = "";
    document.Input.results.value = "";
}

function getResults() {
    checkNumber();
}
    </script>
</body>

3 个答案:

答案 0 :(得分:2)

好吧,我想因为这是一个大学/高中任务,你的教授正试图用循环下的提示教你。

<body>
<!-- GAME INSTRUCTIONS -->
<h1>Number Guessing Game</h1>
<p>A random number between 1 and 100 has been generated. Can you guess it?</p>
<!-- FORM (Includes button to confirm guess, input box, and output box) -->
<form id="Input" name="Input">
    <input name="requestInfo" onclick="getResults()" type="button" value="Start Guessing!">
    <input name="newGame" onclick="resetGame()" type="button" value="Start New Game">
</form><!-- JAVASCRIPT START -->
<script type="text/javascript">

// Define variables
var num = Math.floor(Math.random() * 100) + 1;
var turns = 10;

function checkNumber() {
while (turns > 0) {
  guess=prompt("Tell me your guess.", "Your guess: ");
        if (guess == num) {
            turns = 0;
            alert("Congratulations, you won! The mystery number was " + num + ".");
        } else if (guess < num) {
            turns--;
            alert("Your guess was too low. Turns remaining: " + turns);
        } else if (guess > num) {
            turns--;
             alert("Your guess was too high. Turns remaining: " + turns);
        }
}
if (turns==0)
alert ("You failed to guess sadly.");
}

function resetGame() {
turns = 10;
num = Math.floor(Math.random() * 100) + 1;
}

function getResults() {
checkNumber();
}
</script>

答案 1 :(得分:2)

我同意这些看起来有点奇怪 - 显然,使用非模态对话框,你不需要循环。

你可以做的一件事是使用prompt方法(例如:window.prompt("sometext","defaultText");),然后打开模态对话框询问用户,直到剩余猜测的数量为零,或者直到猜对了。这将在循环中起作用。

答案 2 :(得分:1)

这里有一个这样的。确保用户输入一个数字。

<body>
    <!-- GAME INSTRUCTIONS -->
    <h1>Number Guessing Game</h1>
    <p>A random number between 1 and 100 has been generated. Can you guess it? Click button to start game.</p>
        <button type="button" onclick="startNewGame()">Start New Game</button>
    <script type="text/javascript">

// Define variables
var num = Math.floor(Math.random() * 100) + 1;
var turns;

function checkNumber() {
    while (turns > 0) {
        var guess = prompt("Insert your guess");
        if (!guess || isNaN(guess)) {
            alert("Please enter a valid number");
            continue;
        }
        if (guess == num) {
            alert("Congratulations, you won! The mystery number was " + num + ".");
            return;
        } else {
            turns--;
            if (guess < num) {
                alert("Your guess was too low. Turns remaining: " + turns);
            } else if (guess > num) {
                alert("Your guess was too high. Turns remaining: " + turns);
            }
        }
    }
    if (turns == 0) {
        alert("You have lost");
    }
}

function startNewGame() {
    turns = 10;
    num = Math.floor(Math.random() * 100) + 1;
    checkNumber();
}
</script>
</body>