2016-07-14 31 views
1

我已經瀏覽了互聯網上的所有解決方案,但我不確定爲什麼這不會顯示在瀏覽器中。它適用於JavaScript的repl,但由於某種原因,它不會在我的谷歌瀏覽器中打開。Javascript color game

<script>function numbergame() { 
 
\t colors = ["Red", "Green", "Blue", "Orange", "Yellow", "Indigo", "Violet", "Gray", "Black", "White"] 
 
\t var random_color = colors[Math.round(Math.random()*colors.length)] 
 
\t var guesses = 0; 
 
\t var thecolor = false; 
 
\t var playerguess; 
 
\t while (!thenumber) { 
 
\t playerguess = prompt("I am thinking of a number color from this list:\n" +color +"\nCan you guess which one it is?"); 
 
\t guesses+=1; 
 
\t thenumber = checkguess() 
 
}; 
 

 
function checkguess() { 
 

 
\t \t if (!colors.include(playerguess)) {alert("Please enter a valid color"); return false} 
 
\t \t if (playerguess<random_color) {alert("Hint: the answer is lower alphabetically.");return false} 
 
\t \t if (playerguess>random_color) {alert("Hint: the answer is higher alphabetically.");return false} 
 
\t \t else {alert("You've guessed the color! It was " + random_number+" and it took you " + guesses+ " tries."); 
 
\t \t myBody=document.getElementsByTagName("body")[0]; 
 
\t \t myBody.style.background=thecolor; 
 
\t \t return true;} 
 
}</script> 
 

 
<!doctype html> 
 
<html> 
 
<head> 
 
\t Color Game. 
 
</head> 
 
<body onload= "numbergame()"></body> 
 
</html>

+0

看起來您在數字遊戲功能中的while循環之後丟失了一個閉合大括號 – abigwonderful

+0

代碼非常糟糕。缺少大括號,可變範圍問題,未定義變量。可怕。 – ManoDestra

回答

1

沒有結束的括號匹配就行了一句:

while (!thenumber) { 

使用正確的縮進和格式將使像這樣的錯誤更容易找到。

1

你所寫的內容是無效的HTML。

您的<script>標籤必須放置在HTML文檔的<body><head>中,而不是一開始就放在doctype之前。

+0

將「