-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
110 lines (91 loc) · 3.25 KB
/
Copy pathscript.js
File metadata and controls
110 lines (91 loc) · 3.25 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
// Jeffrey Phelps 2018 RGB Color Guesser guessing game
const squares = document.querySelectorAll(".square");
const colorDisplay = document.getElementById("colorDisplay");
const messageDisplay = document.querySelector("#message");
const h1 = document.querySelector("h1");
const resetBtn = document.querySelector("#resetBtn");
const easyBtn = document.querySelector("#easyBtn");
const hardBtn = document.querySelector("#hardBtn");
let numberOfSquares = 6;
let colors = generateRandomColors(numberOfSquares);
let pickedColor = pickColor();
colorDisplay.textContent = pickedColor;
for(let i = 0; i < colors.length; i++) {
squares[i].style.backgroundColor = colors[i];
squares[i].addEventListener("click", function(){
let clickedColor = this.style.backgroundColor;
if(clickedColor === pickedColor) {
messageDisplay.textContent = "Correct!";
messageDisplay.style.color = clickedColor;
resetBtn.textContent = "Play Again?";
changeColors(clickedColor);
h1.style.backgroundColor = clickedColor;
} else {
this.style.backgroundColor = "#232323";
messageDisplay.textContent = "Try Again";
messageDisplay.style.color = "red";
};
});
};
function changeColors(color){
for(let i = 0; i < squares.length; i++){
squares[i].style.backgroundColor = color;
};
};
function pickColor(){
let randomColor = Math.floor(Math.random() * colors.length);
return colors[randomColor];
};
function generateRandomColors(num){
let arr = [];
for(var i = 0; i < num; i++){
arr.push(randomColor());
}
return arr;
};
function randomColor(){
let reds = Math.floor(Math.random() * 256);
let greens = Math.floor(Math.random() * 256);
let blues = Math.floor(Math.random() * 256);
return "rgb(" + reds + ", " + greens + ", " + blues + ")";
};
// BUTTONS
resetBtn.addEventListener("click", function(){
colors = generateRandomColors(numberOfSquares);
pickedColor = pickColor();
colorDisplay.textContent = pickedColor;
resetBtn.textContent = "New Colors";
h1.style.backgroundColor = "steelblue";
messageDisplay.style.color = "inherit";
messageDisplay.textContent = "Pick a Color!"
for(let i = 0; i < squares.length; i++){
squares[i].style.backgroundColor = colors[i];
};
});
easyBtn.addEventListener("click", function(){
easyBtn.classList.add("selected");
hardBtn.classList.remove("selected")
numberOfSquares = 3;
colors = generateRandomColors(numberOfSquares);
pickedColor = pickColor();
colorDisplay.textContent = pickedColor;
for(let i = 0; i < squares.length; i++){
if(colors[i]){
squares[i].style.backgroundColor = colors[i];
} else {
squares[i].style.display = "none";
}
}
});
hardBtn.addEventListener("click", function(){
easyBtn.classList.remove("selected");
hardBtn.classList.add("selected");
numberOfSquares = 6;
colors = generateRandomColors(numberOfSquares);
pickedColor = pickColor();
colorDisplay.textContent = pickedColor;
for(let i = 0; i < squares.length; i++){
squares[i].style.backgroundColor = colors[i];
squares[i].style.display = "block";
}
});