gpt4 book ai didi

javascript - 如何将背景设置为您选择的颜色

转载 作者:行者123 更新时间:2023-12-02 15:19:42 26 4
gpt4 key购买 nike

功能:

用户玩一个游戏,系统将从数组列表中随机选择一种颜色。当用户选择正确的颜色时,背景将显示正确的颜色,并显示祝贺消息

已完成的工作:

已设置 while 循环对条件进行连续检查,并在 check_guess() 方法内完成验证检查。已设置myBody=document.getElementsByTagName("body")[0];
myBody.style.background = guess_input;
显示系统选择的正确颜色。

问题:

我无法显示系统选择的正确颜色。它不断显示第一个条件警报消息:“抱歉,我无法识别您的颜色。请重试”。

其次,即使我设置了正确的颜色或数组中的任何颜色,我仍然收到“抱歉,我无法识别您的颜色。请重试”的提示

我做错了什么以及为什么它不显示系统选择的颜色

<html>

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

<body onload="do_game()">
<script>
var color = ["blue", "cyan", "gray", "green", "magenta", "orange", "red", "white", "yellow"];
var guess_input_text, guess_input, finished = false,
target, guesses = 0;

function do_game() {

var rnd = Math.floor((Math.random() * 9) + 0);
target = color[rnd];
alert(target);

while (!finished) {
guess_input_text = prompt("I am thinking of these colors:" +
"\n\n blue, cyan, gray, green, magenta, orange, red, white, yellow" + "\n\n what color am I thinking of?");
guess_input = parseInt(guess_input_text);
guesses += 1;
finished = check_guess();
}
}

function check_guess(){

if (guess_input_text != color){
alert("Sorry, i don't recognise your color"+"\n Please try again");
return false;
} else if(guess_input_text == color){
if(guess_input> target){
alert("Sorry, your guess is not correct!" +"\n Hint: Your color is alphabatically higher than mine" + "\n Please try again");
return false;
}else if(guess_input< target){
alert("Sorry, your guess is not correct!" +"\n Hint: Your color is alphabatically lower than mine" + "\n Please try again");
return false;
}
}else{
alert ("Congratulations! You have guessed the color!"+"\nIt took you"+ guesses+" guesses to finish the game!" + "\n You can see the color in the background");
document.body.style.backgroundColor = guess_input;
return true;
}
}
</script>
</body>

</html>

最佳答案

你做错了:

myBody.style.background = guess_input;

我认为你忘记了:

myBody.style.backgroundColor = guess_input;

哦,当你说:

if (guess_input != color) {
alert("Sorry, i don't recognise your color" + "\n Please try again");
return false;
}

您正在将整数与数组进行比较。您必须将整数与数组的位置进行比较(而不是与该位置的数据进行比较)

关于javascript - 如何将背景设置为您选择的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34177138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com