2015-11-05 118 views
0

我正在通過Coursera課程,我們應該建立一個顏色猜謎遊戲。他們給你的基本結構等參考,但我碰到了一些障礙。我的調試器說我在第27行有7個錯誤。隨機顏色猜測遊戲(JavaScript)

基本上,我不能讓警報彈出,我不確定我的代碼是否正常運行,因爲它。我也不確定它是否對數組進行排序併爲每個元素分配一個數字值來創建遊戲的隨機性。

如果有人能夠一步步解釋/解決問題,我會非常感激。

//Global Variables // 
 
var target; 
 
var guess_input; 
 
var guess_input_text; 
 
var finished = false; 
 
var guesses = 0; 
 
var colors = ["blue", "greeen", "red", "yellow", "orange", "purple"]; 
 

 
var sorted_colors = random_color.sort(); 
 

 
//Function 1// 
 
function do_game() { 
 
    //Is this right? // 
 
    target = sorted_colors[Math.floor(Math.random() * sorted_colors.length)]; 
 

 
    while (!finished) { 
 
    guess_input_text = prompt(" I am thinking of one of these colors: \n\n" + colors.join(",") + 
 
     "\n\n What color am I thinking of?"); 
 

 
    guess_input = guess_input_text; 
 
    guesses += 1; 
 
    finished = check_guess(); 
 
    } 
 

 
    //Function 2 // 
 
    function check_guess(guess_input) { 
 
    if sorted_colors.indexOf(guess_input) == -1); { 
 
    alert("Sorry, I don't recoginize your color. \n\n" + "Please try again."); 
 
    return false; 
 
    } 
 
} 
 
} 
 

 
// If Statement 1 // 
 
if (guess_input > target) { 
 
    alert("Sorry, your guess is not correct!\n\n " + "Hint: your color is alphabetically higher than mine. \n\n " + "Please try again."); 
 
    return false; 
 
} 
 

 
// If Statement 2 // 
 
if (guess_input < target) { 
 
    alert("Sorry, your guess is not correct! \n\n " + "Hint: your color is alphabetically lower than mine. \n\n" + "Please try again."); 
 
    return false; 
 
} 
 

 
//If Statement 3 (positive) // 
 
if (guess_input == target) { 
 
    alert("Congratulations! You have guessed the color! \n\n" + "It took you " + guesses + " to finish the game! \n\n" + "You can the color in the background."); 
 
    return true; 
 
}
<!DOCTYPE html> 
 

 
<head> 
 
    <title>Color Guessing Game</title> 
 
</head> 
 

 
<body onload="do_game()"> 
 
    <script src="js_guessing_game.js"> 
 
    </script> 
 
</body> 
 

 
</html>

+0

'如果排序...'應該是'如果(排序....'等等先修復你的錯誤 –

+1

好的,我沒有注意到它謝謝你! –

+0

這應該基本上解決你所有的錯誤:)如果這是一個錯字,歡迎**刪除你的問題**。 –

回答

0

給你放錯了 「);」

把這一個:

function check_guess(guess_input) { 
    if (sorted_colors.indexOf(guess_input) == -1) { 
    alert("Sorry, I don't recoginize your color. \n\n" + "Please try again."); 
    return false; 
    } 

,而不是這一個:

function check_guess(guess_input) { 
    if sorted_colors.indexOf(guess_input) == -1); { 
    alert("Sorry, I don't recoginize your color. \n\n" + "Please try again."); 
    return false; 
    } 
+0

不,請不要使用建議'if sorted_colors.indexOf(guess_input) == -1);'。 –

2

這是你的代碼:

//Function 2 // 
    function check_guess(guess_input) { 
    if sorted_colors.indexOf(guess_input) == -1); { 
    alert("Sorry, I don't recoginize your color. \n\n" + "Please try again."); 
return false; 
} 

第27行:

if sorted_colors.indexOf(guess_input) == -1); { 

您在排序前缺少一個左括號,您需要在右括號後面刪除分號並且您的代碼現在應該如此。

if (sorted_colors.indexOf(guess_input) == -1) { 

不用擔心這樣的事情會發生,而你學習他們只是有點語法錯誤古德勒克人:)