-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorguess.html
74 lines (65 loc) · 1.54 KB
/
colorguess.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html>
<html>
<head>
<title>Color Guess</title>
</head>
<body>
<script type="text/javascript">
colors = ['Red','Blue','Green','Aqua','Chocolate','Cyan','Teal','Orchid','Lavender','Gold','Brown','Indigo','Maroon',
'Yellow','Tomato','Ivory'];
colors.sort();
function do_game() {
var guess=0;
rand = 17*Math.random();
rand = Math.floor(rand);
target = colors[rand];
while(1){
input = prompt('I am thinking of one of these colors:\n' + colors.join(',')+'\nWhat color am I thinking of?');
if(guess==0)
alert(target);
guess++;
check_guess(input,target,rand);
if(target.toUpperCase()==input.toUpperCase()){
ans(target);
alert('Congratulations! You have guessed the color it took you '+guess+' guesses to beat the game.\nYou can see the color in the background');
break;}
else
continue;
}
}
do_game();
function check_guess(input,target,rand) {
var i,flag=-1;
if(input==null)
{
}
for(i=0;i<colors.length;i++)
{
if(input.toUpperCase()==colors[i].toUpperCase())
{
flag=i;
break;
}
}
if(flag>-1)
{
if(rand<flag)
{
alert('Sorry your color is not correct!\nHint: Your color is alphabetically higher than mine.\nPlease try again.');
}
if(rand>flag)
{
alert('Sorry your color is not correct!\nHint: Your color is alphabetically lower than mine.\nPlease try again.');
}
}
if(flag==-1)
{
alert("Sorry I didn't recognize your color\nPlease try again");
}
}
function ans(target) {
document.body.style.backgroundColor = target;
}
</script>
</body>
</html>