2016-11-26 6 views
1

問題は次のとおりです:このコードを実行するたびにうまくいきますが、whileループを実行して間違ったときにゲームを繰り返すことはできません。 。色の推測ゲームが間違ったときにループしない

<!DOCTYPE html> 
<html> 
<head> 
    <title>Color guessing game</title> 
</head> 

<body onload="do_game()"> 
    <script> 
     var color = ["Black","Blue","Brown","Cyan","GoldenRod","Green","Maroon","Olive","Pink","Red"]; 
     var target; 
     var finished = false; 
     var guess_input; 
     var guesses; 

     function do_game() { 
      var target_index = Math.random() * 10; 
      var target = Math.floor(target_index); 

      alert(color[target]); 

      while(!finished) { 
       guess_input = prompt("I am thinking of one of these colors: \n\n" + 
             "Black,Blue,Brown,Cyan,GoldenRod,Green, Maroon,Olive,Pink,Red \n\n" 
             + "What color am i thinking of?"); 
       guesses +=1; 
       finished = check_guess(); 
       } 
     } 

     myBody.style.background=name_of_color; 
    </script> 

</body> 
</html> 
+0

GAH、ゲームを終了する方法はありませんが...幸いにも、それ以上のアラートオプションを示していないがある:)しかし、その後のページ凍結による(真)しばらく 'へ{廃棄cpu} 'キャンセルするためにコードを編集して中止するか空の文字列をチェックする – Endless

答えて

0

あなたの質問には欠けているものはほとんどありません(私たちはcheck_guessって何ですか?)、私はいくつかの仕事をして、あなたが探しているものにかなり近づけようとしました。このソリューションがあなたに役立つことを願っています。

var color = ["Black", "Blue", "Brown", "Cyan", "GoldenRod", "Green", "Maroon", "Olive", "Pink", "Red"]; 
 
var target; 
 
var finished = false; 
 
var guess_input; 
 
var guesses; 
 
var randomComputerGuess; 
 

 
function do_game() { 
 
    var target_index = Math.floor(Math.random() * 10); 
 

 
    alert(color[target_index]); 
 
    randomComputerGuess = color[target_index]; 
 

 
    while (!finished) { 
 
    guess_input = prompt("I am thinking of one of these colors: \n\n" + 
 
     "Black,Blue,Brown,Cyan,GoldenRod,Green, Maroon,Olive,Pink,Red \n\n" + "What color am i thinking of?"); 
 
    if (randomComputerGuess === guess_input) { 
 
     alert("Good Job! You guessed it."); 
 
     finished = true; 
 
     //document.style.background=color[target_index]; 
 
    } 
 
    } 
 
}
<body onload="do_game()">

0

関数check_guessは決して定義していません。

ブラウザがその行のコードを実行しようとすると、エラーが発生してスクリプトが停止しました。

ブラウザで「開発ツール」などのメニュー項目を探してください。エラーが表示されます。

私の推測では、あなたのスクリプトには、myBodyname_of_colorの変数にも問題があります。これらもスクリプトには表示されません。しかし、あなたが「それはうまくいく」と言って以来、おそらくあなたが私たちに示していないことがいくつかあります。

関連する問題