-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
68 lines (59 loc) · 2.04 KB
/
app.js
File metadata and controls
68 lines (59 loc) · 2.04 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
let colors = generateRandomColors(6);
const squares = document.querySelectorAll('.square');
let pickedColor = pickColor();
const colorDisplay = document.querySelector('#colorDisplay');
const messageDisplay = document.querySelector('#message');
const h1 = document.querySelector('h1');
const resetButton = document.querySelector('#reset');
resetButton.addEventListener('click', function(){
resetButton.textContent = 'New colors'
colors = generateRandomColors(6);
pickedColor = pickColor();
colorDisplay.textContent = pickedColor;
for (let i = 0; i < squares.length; i++){
squares[i].style.backgroundColor = colors[i];
}
h1.style.backgroundColor = '#232323';
});
colorDisplay.textContent = pickedColor;
for(let i = 0; i < squares.length; i++){
//add initial colors to squares
squares[i].style.backgroundColor = colors[i];
//add listeners to squares
squares[i].addEventListener('click', function() {
//alert(this.style.backgroundColor);
//alert(squares[i].style.backgroundColor);
let clickedColor = squares[i].style.backgroundColor;
if(clickedColor === pickedColor){
messageDisplay.textContent = 'Correct';
changeColors(clickedColor);
h1.style.backgroundColor = pickedColor;
resetButton.textContent = 'Play again'
} else {
this.style.backgroundColor = '#232323';
messageDisplay.textContent = 'Try again';
}
});
}
function changeColors(color){
for (let i = 0; i < squares.length; i++){
squares[i].style.backgroundColor = color;
}
}
function pickColor(){
let random = Math.floor(Math.random() * colors.length);
return colors[random];
}
function generateRandomColors(num){
const arr = [];
for(let i = 0; i < num; i++){
arr.push(randomColor());
}
return arr;
}
function randomColor(){
let r = Math.floor(Math.random() * 256);
let g = Math.floor(Math.random() * 256);
let b = Math.floor(Math.random() * 256);
return (`rgb(${r}, ${g}, ${b})`);
}