如何将背景设置为您选择的颜色
how to set background to color you have chosen
功能:
用户玩一个游戏,系统将从数组中的列表中随机选择一种颜色。当用户选择正确的颜色时,背景将显示正确的颜色,并将显示祝贺信息
已经做了什么:
设置while循环对条件进行连续检查,并在check_guess()方法中进行验证检查。已将CCD_ 1设置为显示系统选择的正确颜色。
问题:
我无法显示系统选择的正确颜色。它一直显示第一个条件警报消息:"对不起,我认不出你的颜色。请再试一次"。
其次,即使我设置了正确的颜色或阵列中的任何颜色,我也会不断收到"对不起,我认不出你的颜色。请再试一次"的提示
我做错了什么,为什么它没有显示系统选择的颜色
<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;
}
您正在比较一个整数和一个数组。您必须将整数与数组的位置(而不是该位置的数据)进行比较
首先,行if (guess_input != color) {
将失败,因为color
是一个数组。要检查guess_input
是否在color
阵列中,我建议使用
if (color.indexOf(guess_input) < 0) {
array_variable.indexOf(value_to_check_for)
方法将返回在array_variable
中找到value_to_check_for
的索引,如果根本没有找到,则返回-1。有关更多详细信息和用法示例,请参阅数组#indexOf。
这方面的另一个问题是,您试图将字符串输入解析为一个整数,您不需要它。您可以更改
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);
可以更改为:
guess_input = 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?");
其次,您可能需要更换myBody = document.getElementsByTagName("body")[0];
myBody.style.background = guess_input;
具有CCD_ 10作为CCD_。
完成的文章看起来像:
<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 = 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?");
guesses += 1;
finished = check_guess();
}
}
function check_guess() {
if (color.indexOf(guess_input) < 0) {
alert("Sorry, i don't recognise your color" + "'n Please try again");
return false;
}
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;
}
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;
}
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>
- 每个选择器的Jquery css颜色都在变化,但字体大小却没有变化
- Javascript对象类在单击时打开窗口进行颜色选择,并在更改时替换对象背景颜色
- DT数据表背景颜色为黑色,但仍有悬停和选择颜色
- 通过Ember颜色选择器更新SCSS变量
- 如何动态创建许多Jquery颜色选择器(eyecon)
- 选择字段-更改popover颜色
- Javascript没有从数组中选择背景颜色
- HighCharts:3D柱形图在选择时更改边框颜色
- 每次单击按钮时随机选择颜色
- 使用 Javascript 选择颜色值
- 在Wordpress中随机选择颜色
- 单击该选项后更改滑块以选择颜色
- HTML Imagemap与PHP和Javascript /用户可以选择颜色
- 使用onchange事件获取选择颜色列表的值,以便更改图片
- 获取当前浏览器中的文本选择颜色和背景颜色
- 如何在量角器的颜色选择器窗口中选择颜色
- 使用javascript从HTML中线性渐变提供的范围中选择颜色
- AngularJs -使用$parent ng-model从控制器更改/推送选择颜色
- 用dat设置三个js中的一个对象的颜色.GUI选择颜色
- Javascript画布绘图应用程序选择颜色