2015-11-05 120 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) { 

不用担心这样的事情会发生,而你学习他们只是有点语法错误古德勒克人:)