2017-05-06 6 views
1

割り当てのために、私はJS番号推測ゲームを作成する必要があります。ユーザーの推測を確認するためのループとリセットゲームボタンが必要です。私の問題は、ループを動作させることです。私はターン数を10で開始したい。ユーザーが間違った推測をするたびに、ターン数は1減少し、正しく推測すればターン数は0になる。 "Start New Game"ボタンを押すと、新しい番号が生成され、ターン数は10にリセットされます。Javascript番号推測ゲームにループを挿入する

ループは、特にwhileループである必要はありません。誰か助けてくれますか?それはあなたの教授がループの下にプロンプ​​トを使って、あなたを教えしようとしている大学/ 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="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

さてさて、私は推測します。

<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> 
関連する問題