-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorGame.js
101 lines (89 loc) · 2.98 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
var squares = document.querySelectorAll(".square");
var colorElem = document.getElementById("color");
var message = document.getElementById("message");
var h1 = document.querySelector("h1");
var resetBtn = document.querySelector("#reset");
var slider = document.getElementById("difficulty");
var colors = generateColorArray(slider.value);
var colorPicked = pickColor();
colorElem.textContent = colorPicked;
initalize()
function initalize() {
setupSlider();
setupResetButton();
addListenersToSquares();
}
function setupSlider() {
slider.addEventListener('change', function() {
var node = document.getElementById('container');
node.innerHTML = '';
for (var i = 0; i < slider.value; i++) {
addElement();
}
colors = generateColorArray(slider.value);
colorPicked = pickColor();
colorElem.textContent = colorPicked;
addListenersToSquares();
});
}
function setupResetButton() {
resetBtn.addEventListener("click", function() {
colors = generateColorArray(slider.value);
colorPicked = pickColor();
colorElem.textContent = colorPicked;
for (var i = 0; i < colors.length; i++) {
squares[i].style.backgroundColor = colors[i];
}
h1.style.backgroundColor = "#3492eb";
resetBtn.textContent = "New Colors";
message.textContent = "";
});
}
function changeAllColors(color) {
for (var i = 0; i < squares.length; i++) {
squares[i].style.backgroundColor = color;
}
}
function pickColor() {
const rand = Math.floor(Math.random() * colors.length);
return colors[rand];
}
function generateColorArray(num) {
var arr = []
for (var i = 0; i < num; i++) {
arr.push(generateRandomColor())
}
return arr;
}
function generateRandomColor() {
const red = Math.floor(Math.random() * 256);
const green = Math.floor(Math.random() * 256);
const blue = Math.floor(Math.random() * 256);
return "rgb("+red+", "+green+", "+blue+")";
}
function addElement() {
// Adds an element to the document
var p = document.getElementById('container');
var newElement = document.createElement('div');
newElement.setAttribute('class', 'square');
newElement.setAttribute('style', 'background-color: rbg(255, 0, 0);');
p.appendChild(newElement);
}
function addListenersToSquares() {
squares = document.querySelectorAll(".square");
for (var i = 0; i < squares.length; i++) {
squares[i].style.backgroundColor = colors[i];
squares[i].addEventListener("click", function() {
if (this.style.backgroundColor === colorPicked) {
changeAllColors(colorPicked);
h1.style.backgroundColor = colorPicked;
message.textContent = "Correct";
resetBtn.textContent = "Play Again?";
}
else {
this.style.backgroundColor = "#232323";
message.textContent = "Try again";
}
});
}
}