-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorgame.js
More file actions
100 lines (95 loc) · 2.99 KB
/
colorgame.js
File metadata and controls
100 lines (95 loc) · 2.99 KB
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
var squares=document.querySelectorAll(".square");
var colors = generaterandomcolors(6);
var pickedColor = randomcolor(6);
var messageDisplay=document.getElementById("gameStatus");
var display = document.getElementById("colorDisplay");
var h1 = document.querySelector("h1");
var resbut = document.querySelector("#res");
var easbtn = document.querySelector("#eas");
var hrdbtn = document.querySelector("#har");
var numberofsq = 6;
easbtn.addEventListener("click", function(){
easbtn.classList.add("selected");
hrdbtn.classList.remove("selected");
numberofsq = 3;
colors = generaterandomcolors(numberofsq);
pickedColor = randomcolor(numberofsq);
display.textContent = pickedColor;
for (var i = 0; i < squares.length; i++) {
if(colors[i]){
squares[i].style.backgroundColor = colors[i];
}
else{
squares[i].style.display = "none";
}
}
});
hrdbtn.addEventListener("click", function(){
numberofsq = 6;
hrdbtn.classList.add("selected");
easbtn.classList.remove("selected");
colors = generaterandomcolors(numberofsq);
pickedColor = randomcolor(numberofsq);
display.textContent = pickedColor;
for (var i = 0; i < squares.length; i++) {
if(colors[i]){
squares[i].style.backgroundColor = colors[i];
squares[i].style.display = "block";
}
}
});
resbut.addEventListener("click", function(){
colors=generaterandomcolors(numberofsq);
pickedColor=randomcolor(numberofsq);
display.textContent = pickedColor;
messageDisplay.textContent = "";
this.textContent = "New colors!"
for(var i=0;i<squares.length;i++){
squares[i].style.backgroundColor=colors[i];
}
h1.style.backgroundColor= steelblue;
resbut.textContent="New colors";
})
display.textContent = pickedColor;
//squares.forEach(function(thi){
// thi.style.backgroundColor=colors[i];
// i++;
//});
for (var i = 0; i < squares.length; i++) {
squares[i].style.backgroundColor = colors[i];
squares[i].addEventListener("click", function(){
var clickedColor=this.style.backgroundColor;
console.log(clickedColor,pickedColor)
if(clickedColor === pickedColor){
h1.style.backgroundColor=clickedColor;
resbut.textContent="Play Again";
for(var j=0; j< squares.length; j++){
squares[j].style.backgroundColor = pickedColor;
}
messageDisplay.textContent = "Correct option";
}
else{
this.style.backgroundColor = "#212211";
messageDisplay.textContent = "Try again!";
}
});
}
function generaterandomcolors(num){
//make an array
var arr=[];
for(var i=0; i<num; i++){
arr.push(randomColor());
}
return arr;
}
function randomColor(){
//to pick r,g,b from 0 to 255
var r = Math.floor(Math.random() * 256);
var g = Math.floor(Math.random() * 256);
var b = Math.floor(Math.random() * 256);
return "rgb(" + r + ", " + g + ", " + b + ")";
}
function randomcolor(n){
var rand = Math.floor(Math.random() * n);
return colors[rand];
}