-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorGame.js
109 lines (92 loc) · 3.61 KB
/
ColorGame.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var noofsquare=6;
var color = generateRandomcolor(noofsquare);
var square = document.querySelectorAll(".square");
var pickedcolor= pickcolor();
var colordisplay =document.getElementById("colordisplay");
var messagedisplay=document.getElementById("messagedisplay");
var h1=document.querySelector("h1");
var resetbut=document.querySelector("#reset");
var mode=document.querySelectorAll(".mode");
initialize();
function initialize(){
for(var i=0;i<mode.length;i++)
{
mode[i].addEventListener("click",function(){
mode[0].classList.remove("selected");
mode[1].classList.remove("selected");
this.classList.add("selected");
this.textContent === "Easy" ? noofsquare=3 : noofsquare=6;
reset();
});
}
for(var i=0;i<square.length;i++)
{
//add click listeners to squares
square[i].addEventListener("click",function(){
//grab color of clicked square
var clickedcolor = this.style.background;
//compare to picked color
console.log(clickedcolor, pickedcolor);
if(clickedcolor === pickedcolor){
messagedisplay.textContent = "Yay! You are Correct";
changecolor(clickedcolor);
h1.style.background = clickedcolor;
resetbut.textContent="PLAY AGAIN?"
}
else{
this.style.background="#232323";
messagedisplay.textContent="Try Again";
}
});
}
reset();
}
function reset(){
//generate all new colors
color=generateRandomcolor(noofsquare);
//pick a new random color
pickedcolor=pickcolor();
//change colors of colordisplay to match picked color
colordisplay.textContent= pickedcolor;
//chnage colors of square
for(var i=0;i<square.length;i++)
{
if(color[i]){square[i].style.display="block"; square[i].style.background = color[i];}
else{square[i].style.display="none";}
}
messagedisplay.textContent="";
h1.style.background="steelblue";
resetbut.textContent="New Colors";
}
resetbut.addEventListener("click",function(){
reset();
});
colordisplay.textContent=pickedcolor;
function changecolor(color){
for(var j=0;j<square.length;j++)
{ //make same color of all the squares
square[j].style.background=color;
}
}
function pickcolor(){
var random= Math.floor(Math.random() * color.length);
return color[random];
}
function generateRandomcolor(num){
//make array
var arr = [];
for(var i = 0 ; i < num ; i++)//get random color & push into arr
{
arr.push(randomcolor())
}
return arr;
}
function randomcolor(){
//Pick a color for red
var r=Math.floor(Math.random() *256);
// Pick a color for blue
var b=Math.floor(Math.random() *256);
//Pick a color for green
var g=Math.floor(Math.random() *256);
return "rgb(" + r + ", " + b + ", " + g + ")";
}