2017-05-06 61 views
1

對於一個任務,我需要做一個JS數字猜謎遊戲。它需要包含一個循環來檢查用戶的猜測和重置遊戲按鈕。我的問題是讓循環工作。我想讓轉數從10開始。每次用戶做出不正確的猜測時,他們的轉數減少1,如果他們猜測正確,他們的轉數爲0.如果他們推動「開始新遊戲」按鈕,應該生成一個新的數字並且圈數應該被重置爲10.將一個循環插入一個Javascript數字猜謎遊戲

該循環並不需要是一個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> 

回答

2

好的,我猜是因爲這是一個大學/ HS的任務,你的教授試圖教你使用提示下的循環。

<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> 

+0

我會刪除循環,如果我可以。在學習老師需要循環完成作業之前,我做了完全相同的事情。 – Emily

+0

如果你需要循環,你必須改變邏輯。想讓我把它寫出來嗎? –

+0

如果可以的話,那會很棒 – Emily

2

我同意TAKS似乎有點不可思議 - 顯然,與非模態對話框,你不會需要一個循環。

你可以做的一件事是使用prompt方法(例如:window.prompt("sometext","defaultText");),然後打開一個模式對話框詢問用戶,直到剩餘猜測的數量爲零,或者直到猜測正確爲止。這將在循環中起作用。

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>