-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
132 lines (113 loc) · 3.75 KB
/
script.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
124
125
126
127
128
129
130
131
132
var colors = []
var numberOfSquares = 9;
var colors = generateRandomColors(numberOfSquares);
var squares = document.querySelectorAll(".square");
var randomAnswer = pickedColor();
var rgbDisplay = document.getElementById("rgbDisplay");
var resetButton = document.querySelector("#reset");
var easyBtn = document.querySelector("#easyBtn");
var mediumBtn = document.querySelector("#mediumBtn");
var hardBtn = document.querySelector("#hardBtn");
// easy button removes selected class from other buttons plus sqares
easyBtn.addEventListener("click", function() {
hardBtn.classList.remove("selected");
mediumBtn.classList.remove("selected");
easyBtn.classList.add("selected");
difficultyChanged(3);
});
// medium button removes selected class from other buttons plus sqares
mediumBtn.addEventListener("click", function() {
hardBtn.classList.remove("selected");
mediumBtn.classList.add("selected");
easyBtn.classList.remove("selected");
difficultyChanged(6);
});
// hard button removes selected class from other buttons plus sqares
hardBtn.addEventListener("click", function() {
hardBtn.classList.add("selected");
mediumBtn.classList.remove("selected");
easyBtn.classList.remove("selected");
difficultyChanged(9);
});
// resets colors
resetButton.addEventListener("click", function() {
colors = generateRandomColors(numberOfSquares);
reset();
// change square colors
for (var i = 0; i < squares.length; i++) {
squares[i].style.backgroundColor = colors[i];
}
})
rgbDisplay.textContent = randomAnswer;
for (var i = 0; i < squares.length; i++) {
// adds initial colors
squares[i].style.backgroundColor = colors[i];
//click listeners to squares and compare to randomAnswer
squares[i].addEventListener("click", function() {
var clickedColor = this.style.backgroundColor;
if (clickedColor === randomAnswer) {
messageCenter.textContent = "Correct!";
resetButton.textContent = "Play Again?";
changeColors(clickedColor);
gameTitle.style.backgroundColor = clickedColor;
} else {
this.style.backgroundColor = "#232323";
messageCenter.textContent = "Try Again"
}
});
}
// won and change colors
function changeColors(color) {
for (var i = 0; i < squares.length; i++) {
squares[i].style.background = color;
}
}
//choose a random color picked to be answer
function pickedColor() {
var random = Math.floor(Math.random() * colors.length);
return colors[random];
}
//generates "num" random colors and adds numbers to an array
function generateRandomColors(num) {
var arr = []
for (var i = 0; i < num; i++) {
arr.push(randomPicker())
}
return arr;
}
// generates the array of random colors
function randomPicker() {
// red 0 to 255
randomRed = Math.floor(Math.random() * 256);
// green 0 to 255
randomGreen = Math.floor(Math.random() * 256);
// blue 0 to 225
randomBlue = Math.floor(Math.random() * 256);
return "rgb(" + randomRed + ", " + randomGreen + ", " + randomBlue + ")";
}
// sets the number of squares chosen by difficulty selected and chooses x number of sqares
function difficultyChanged(difficulty) {
numberOfSquares = difficulty
colors = generateRandomColors(difficulty);
reset();
squareSetup();
}
// resets the platform for any changes that would effect the game
function reset() {
randomAnswer = pickedColor();
rgbDisplay.textContent = randomAnswer;
messageCenter.textContent = "";
gameTitle.style.backgroundColor = "steelblue";
resetButton.textContent = "New Colors?"
}
// setup sqares for change in difficulty or squares present
function squareSetup() {
for (var i = 0; i < squares.length; i++) {
if (colors[i]) {
squares[i].style.backgroundColor = colors[i];
squares[i].style.display = "block"
} else {
squares[i].style.display = "none"
}
}
}