-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_game.js
52 lines (46 loc) · 906 Bytes
/
color_game.js
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
var colors=[];
for(var i=0;i<6;i++)
{
colors[i]=generatecolor();
}
var square=document.querySelectorAll(".square");
function colorgenerate()
{
var c=Math.floor((Math.random()*6)+1);
return colors[c];
}
var picked=colorgenerate();
var update=document.querySelector("#update");
update.textContent=picked;
function generatecolor()
{
var r=Math.floor(Math.random()*256);
var g=Math.floor(Math.random()*256);
var b=Math.floor(Math.random()*256);
var l="rgb("+r+", "+g+", "+b+")";
return l;
}
for(var j=0;j<square.length;j++)
{
square[j].style.background=colors[j];
}
for(var k=0;k<6;k++)
{
square[k].addEventListener("click",function()
{
var choose=this.style.background;
if(choose===picked)
{
alert("Hurray,You Won :)");
for(var u=0;u<6;u++)
{
square[u].style.background=picked;
}
}
else
{
this.style.background="#232323";
alert("Try again");
}
});
}