-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorGame.js
125 lines (111 loc) · 2.95 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var colors = [];
var winningColor;
var squareNum = 6;
var squares = document.querySelectorAll(".square");
var colorHead = document.querySelector("#winningColor");
var messageDisplay = document.querySelector("#message");
var h1 = document.querySelector("h1");
var reset = document.getElementById("reset");
var easy = document.getElementById("easy");
var hard = document.getElementById("hard");
var modeButtons = document.querySelectorAll(".mode");
init();
//FUNCTIONS
function init(){
//mode buttons
setUpModeButtons();
// square event listeners
setUpSquares();
// new game button
setUpResetButton();
// initializes the first color options
resetGame();
}
function setUpModeButtons(){
for (var i = 0; i < modeButtons.length; i++) {
modeButtons[i].addEventListener("click", function() {
modeButtons[0].classList.remove("selected");
modeButtons[1].classList.remove("selected");
this.classList.add("selected");
this.textContent === "Easy" ? squareNum = 3 : squareNum = 6;
resetGame();
});
}
}
function setUpSquares() {
for (var i = 0; i < squares.length; i++) {
//add initial colors to squares
squares[i].style.background = colors[i];
//add click listeners to squares
squares[i].addEventListener("click", function(){
if(this.style.background === winningColor){
changeToWinningColors(winningColor);
}
else{
this.style.background = "#232323";
messageDisplay.textContent = "try again";
}
});
}
}
function setUpResetButton(){
reset.addEventListener("click", function() {
resetGame();
});
}
//resets the game
function resetGame(){
colors = setColors(squareNum);
winningColor = pickColor();
for (var 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";
}
}
h1.style.background = "steelblue";
reset.textContent = "New Colors";
messageDisplay.textContent = "";
}
//returns an array of random colors as long as the num specified
function setColors(num){
var arr = [];
for (var i = 0; i < num; i++) {
arr.push(randomColor());
// TESTER: console.log(arr[i]);
}
return arr;
}
// used in setColor() function. returns an random color formatted
// like "rgb(33, 54, 222)"
function randomColor(){
var nums = [];
var str ="";
var color;
for (var j = 0; j< 3; j++) {
nums.push(Math.floor(Math.random() * (255 + 1)));
}
color = str.concat(
"rgb(",
nums[0],", ",
nums[1],", ",
nums[2], ")");
return color;
}
//picks a winning color in the format "rbg(44, 33, 60)"
function pickColor(){
var randNum = Math.floor(Math.random() * (colors.length));
colorHead.textContent = colors[randNum];
return colors[randNum];
}
//used when the winning color is clicked
function changeToWinningColors(tempColor) {
for (var i = 0; i < squares.length; i++) {
squares[i].style.background = tempColor;
}
h1.style.background = winningColor;
messageDisplay.textContent = " Correct! ";
reset.textContent = "Play Again?";
}