我已經瀏覽了互聯網上的所有解決方案,但我不確定爲什麼這不會顯示在瀏覽器中。它適用於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>
看起來您在數字遊戲功能中的while循環之後丟失了一個閉合大括號 – abigwonderful
代碼非常糟糕。缺少大括號,可變範圍問題,未定義變量。可怕。 – ManoDestra