-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
164 lines (125 loc) · 3.61 KB
/
script.js
File metadata and controls
164 lines (125 loc) · 3.61 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//alert("Connected!");
var new_color_button = document.getElementById("new_colors");
var colors;
var easy_button = document.getElementById("easy");
var hard_button = document.getElementById("hard");
var len = 6;
console.log(easy_button);
initialize();
function hide_blocks(){
var str = len == 3 ? "none": "block";
for(var i=3; i<6; i++){
squares[i].style.display = str;
}
}
function initialize(){
colors = generate_color(len);
goal = random_color();
squares = document.querySelectorAll(".square");
message = document.getElementById("message");
// Display/hide divs according to len
hide_blocks();
//Change RGB text
pick_newColor();
//reset background of h1 and message text
resetH1_message();
new_color_button.textContent ="New Colors";
// paint squares with initial colors
paint_squares();
}
function toggle_hard_easy() {
easy_button.classList.toggle("selected");
hard_button.classList.toggle("selected");
}
easy_button.addEventListener("click", function () {
if(len != 3)
{
toggle_hard_easy();
len = 3;
initialize();
}
});
hard_button.addEventListener("click", function(){
if(len != 6)
{
toggle_hard_easy();
len = 6;
initialize();
}
});
function resetH1_message(){
var h1_background = document.querySelector(".header");
h1_background.style.background = "steelblue";
// change message
message.textContent = "";
}
// paint with new colors when clicked
new_color_button.addEventListener("click", function(){
//alert("Button Clickded");
this.textContent = "New Colors";
// generate new colors
colors = generate_color(len);
// change goal
goal = random_color();
// change picked color text
pick_newColor();
// change background of h1 element
//change message content
resetH1_message();
paint_squares();
//console.log(colors);
});
//Change rgb() text
function pick_newColor() {
var pickedColor = document.getElementById("_goal");
pickedColor.innerText = goal;
}
function generate_color(len){
// make and array
var colors = [];
// add random colors to array
for(var i=0; i<len; i++){
var r = Math.floor(Math.random()*256);
var g =Math.floor(Math.random()*256);
var b =Math.floor(Math.random()*256);
colors.push("rgb(" + r +", "+ g+ ", " +b+")");
//colors[i] = "rgb(" + r +", "+ g+ ", " +b+")";
//console.log(colors[i]);
}
// return array
return colors;
}
function color_all() {
for(var i=0; i<squares.length; i++){
squares[i].style.background = goal;
}
}
function paint_squares(){
for(var i=0; i<squares.length; i++){
// add initial colors
squares[i].style.background = colors[i];
// add event listeners
squares[i].addEventListener("click", function(){
// get the color of clicked square
var guess = this.style.background;
//alert(guess);
if(guess === goal)
{
message.textContent = "Correct";
color_all();
var h1_background = document.querySelector(".header");
h1_background.style.background = goal;
new_color_button.textContent = "Play Again?";
}
else{
this.style.background = "#232323";
message.textContent = "Try Again";
}
// compare it with our goal or picked_color
})
}
}
function random_color() {
var index = Math.floor(Math.random() * colors.length);
return colors[index];
}