我是一个新手JS,我正在做一个游戏,用户必须猜测随机生成的颜色。JS代码猜测游戏颜色不工作
我注意到我试图写错误的代码的结果,也就是说,当我在提示中输入颜色时,即使错误总是说正确的。我在check_guess函数中写了条件时出错了。
我在游戏结束时发现的另一个错误应该是在背景中猜出它的颜色,也不应该。
你能帮我弄清楚我错了吗?
while (!finished) {
guess_input_text = prompt("I am thinking of one of these colors:\n\n" +
colors_message + "\n\n What is the color am I thinking of?");
guess_input = guess_input_text.toLowerCase();
guesses += 1;
finished = check_guess();
}
}
function check_guess() {
if (guess_input == -1) {
alert("Sorry, I don't recognize your color. \n\n Please try again.");
return false;
} else if (guess_input > target) {
alert("Sorry, your guess is not correct!\n\nHint: Your color is alphabetically higher than mine.\n\nPlease try again.");
return false;
} else if (guess_input < target) {
alert("Sorry, your guess is not correct!\n\nHint: Your color is alphabetically lower than mine.\n\nPlease try again.");
return false;
} else {
alert("Congratulations! You have guessed the color!\n\nIt took you " + guesses + " guesses to finish the game!\n\nYou can see the colour in the background.");
var myBody = document.getElementsByTagName("body")[0];
myBody.style.background = target;
return true;
}
}
</script>
我们不能调试整个程序为您服务。必须选择一个问题并编辑您的问题或重新发布只针对该问题每个stackoverflow规则。如果你不解决,他们可能会结束这个问题。 –
@AuroraRuggieri您需要为我们提供更多数据,比如'colors_message','guesses','target','finished'等等。 –
@AuroraRuggieri我收到了我向你询问的数据,并在下面的Q中得到了答案。接受它,如果你发现它是正确的,并关闭此Q –