-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
146 lines (115 loc) · 3.6 KB
/
main.js
File metadata and controls
146 lines (115 loc) · 3.6 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
//Variables
var numSquares = 6;
var colors = generateRandomColors(numSquares);
var pickedColor = pickColor();
//Selectors
var squares = document.querySelectorAll(".square");
var colorDisplay = document.getElementById("colorDisplay");
var messageDisplay = document.querySelector("#message");
var resetBtn = document.querySelector("#resetBtn");
var h1 = document.querySelector("h1");
var modeBtn = document.querySelectorAll(".modeBtn");
init(); //calling init
function init(){
//main flow starts here.
modeBtn[1].classList.add("selected");
reset();
checkColor();
modeListener();
//click listener of Reset Button
resetBtn.addEventListener("click", function(){
reset();
});
}
//function reset
function reset(){
//set background to original color
h1.style.background = "#1b646d";
//hide the Try Again or Correct message
messageDisplay.textContent = "";
resetBtn.textContent = "More Colors";
//change colors array
colors = generateRandomColors(numSquares);
//display colors in squares
for(i=0; i<squares.length; i++){
if(colors[i]){
squares[i].style.display = "block";
squares[i].style.background = colors[i];
}
else{
squares[i].style.display = "none";
}
}
//pick random colorDisplay
pickedColor = pickColor();
//select colorDisplay accordingly
colorDisplay.textContent = pickedColor;
}
function checkColor(){
for(var i=0; i<squares.length; i++){
//adding click listener
squares[i].addEventListener("click", function(){
//grab color of clicked event
var clickedColor = this.style.background;
//compare color to pickedColor
if(pickedColor === clickedColor){
messageDisplay.textContent = "Correct!";
changeColor(pickedColor);
resetBtn.textContent = "Play Again?";
}
else{
this.style.background= "#232323";
messageDisplay.textContent = "Try Again";
}
});
}
}
//Mode listener
function modeListener(){
for( var i=0; i<modeBtn.length; i++){
modeBtn[i].addEventListener("click", function(){
h1.style.background = "#1b646d";
modeBtn[0].classList.remove("selected");
modeBtn[1].classList.remove("selected");
this.classList.add("selected");
this.textContent === "Easy"? numSquares = 3: numSquares = 6;
reset();
});
}
}
//Changes the color of heading and blocks
function changeColor(color){
//changing color of heading
h1.style.background = color;
//changing color of blocks
for(i=0; i<squares.length; i++){
squares[i].style.background = color;
}
}
//Chooses a random color from array
function pickColor(){
var random = Math.floor(Math.random()*colors.length);
return colors[random];
}
//Generates array of colors
function generateRandomColors(arrayLength){
//make a array
var arr = [];
//fill it with colors of given arrayLength
for (i=0; i<arrayLength; i++){
arr.push(randomColor());
}
//return the array
return arr;
}
//Generates a random color with rgb(r, g, b) format
function randomColor(){
//random proportion of red
var r = Math.floor(Math.random()*256);
//random proportion of green
var g = Math.floor(Math.random()*256);
//random proportion of blue
var b = Math.floor(Math.random()*256);
//return a string with rgb(r, g, b) format
return "rgb(" + r + ", " + g + ", " + b + ")";
}