2017-06-03 16 views
0

私は色推測ゲームを作成するように割り当てられています。アイデアは、プレイヤーが色を推測する必要があり、正しく推測すると背景が変わるということです。しかし、私はゲームを起動してブラウザで動かすのに苦労しています。私はコンソールを見て、強調表示されているエラーを理解していません。色推測ゲーム

<body> 
 
<script type="text/javascript"> 
 

 
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> 
 
    <title>Document</title> 
 
</head> 
 
<body> 
 

 
<script type="text/javascript"> 
 

 
window.onload = game() 
 

 
var colours=["blue","purple","pink","red","green","yellow"]; 
 
// var target_index 
 
var target 
 
var guess_input 
 
var finished = false; 
 
var guesses = 0; 
 

 
    function game() { 
 
    var random_colours = Math.floor(Math.random()* colours.length); 
 
    target = random_colours; 
 

 
    while (!finished) { 
 
    guess_input = prompt ("I'm thinking of one of these colours\n" + colours + "\n What colour am I thinking of?") 
 
    guesses += 1; 
 
    finished = check_guess(); 
 
    } 
 
    } 
 

 

 
    function check_guess() { 
 
    if (guess_input == isNaN) { 
 
     alert ("I don't recgonise\n" + "Please try again"); 
 
     return false; 
 
    } 
 

 
    if (guess_input > target) { 
 
     alert ("Your colour is alphabetically higher than mine\n" + "Please try again"); 
 
     return false; 
 
    } 
 

 
    if (guess_input < target) { 
 
     alert ("Your colour is alphabetically lower than mine\n" + "Please try again"); 
 
     return false; 
 
    } 
 
    if (guess_input == target) { 
 
     alert ("Congratulations, the colour was" + target + "it took you" + guesses + "to finish the game!" "You can see the color in the background"); 
 
     return true; 
 
    } 
 
    } 
 

 
</script> 
 
</body> 
 
</html>

答えて

1

間違ったことがいくつかありました。 まず、スニペットには<body><script type="text/javascript">が追加されています。それを取り除く。

第2に、変数が宣言される前にwindow.onloadに関数を関連付けています。これにより、呼び出し時にcolour変数が未定義になります。 window.onload = game()の前にすべての変数を移動してください。

色が正しく表示されるまでループしているのを心配してください。しかし、質問のポーズとしてエラーを修正しています。

ここに作業スニペットがあります。ここにはが実行されません。

var colours=["blue","purple","pink","red","green","yellow"]; 
 
// var target_index 
 
var target 
 
var guess_input 
 
var finished = false; 
 
var guesses = 0; 
 

 
window.onload = game(); 
 

 

 
    function game() { 
 
    var random_colours = Math.floor(Math.random() * colours.length); 
 
    target = random_colours; 
 

 
    while (!finished) { 
 
\t \t guess_input = prompt ("I'm thinking of one of these colours\n" + colours + "\n What colour am I thinking of?") 
 
\t \t guesses += 1; 
 
\t \t finished = check_guess(); 
 
    } 
 
    } 
 

 

 
    function check_guess() { 
 
    if (guess_input == isNaN) { 
 
     alert ("I don't recgonise\n" + "Please try again"); 
 
     return false; 
 
    } 
 

 
    if (guess_input > target) { 
 
     alert ("Your colour is alphabetically higher than mine\n" + "Please try again"); 
 
     return false; 
 
    } 
 

 
    if (guess_input < target) { 
 
     alert ("Your colour is alphabetically lower than mine\n" + "Please try again"); 
 
     return false; 
 
    } 
 
    if (guess_input == target) { 
 
     alert ("Congratulations, the colour was" + target + "it took you" + guesses + "to finish the game!" + "You can see the color in the background"); 
 
     return true; 
 
    } 
 
    }
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> 
 
    <title>Document</title> 
 
</head> 
 
<body> 
 
</body> 
 
</html>