我正在通过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>
'如果排序...'应该是'如果(排序....'等等先修复你的错误 –
好的,我没有注意到它谢谢你! –
这应该基本上解决你所有的错误:)如果这是一个错字,欢迎**删除你的问题**。 –