-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
88 lines (77 loc) · 2.66 KB
/
Copy pathscript.js
File metadata and controls
88 lines (77 loc) · 2.66 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
function createCircle() {
const circlesContainer = document.getElementById('circles-container');
for (let count = 1; count <= 6; count += 1) {
const circle = document.createElement('div');
circle.classList.add('ball');
circlesContainer.appendChild(circle);
}
}
createCircle();
function generateRandomColor() {
const randomNumber1 = Math.floor(Math.random() * 255);
const randomNumber2 = Math.floor(Math.random() * 255);
const randomNumber3 = Math.floor(Math.random() * 255);
return `rgb(${randomNumber1}, ${randomNumber2}, ${randomNumber3})`;
}
const circles = document.getElementsByClassName('ball');
function applySpecificColor(specificColor) {
const colorToGuess = document.getElementById('rgb-color');
colorToGuess.innerText = specificColor;
const randomCircle = circles[Math.floor(Math.random() * circles.length)];
randomCircle.style.backgroundColor = specificColor;
}
function applyRandomColors(specificColor) {
for (let index = 0; index < circles.length; index += 1) {
if (circles[index].style.backgroundColor !== specificColor) {
circles[index].style.backgroundColor = generateRandomColor();
}
}
}
function incrementScore() {
const score = document.getElementById('score').innerText;
const increment = Number(score) + 3;
document.getElementById('score').innerText = increment;
}
const answer = document.getElementById('answer');
function validation() {
for (let index = 0; index < circles.length; index += 1) {
if (circles[index].classList.contains('hitedElement')) {
answer.style.color = 'red';
answer.innerText = 'Reinicie o jogo';
}
}
}
function hitTheColor(event, specificColor) {
validation();
if (answer.innerText !== 'Reinicie o jogo') {
if (event.target.style.backgroundColor === specificColor) {
event.target.classList.add('hitedElement');
answer.innerText = 'Acertou!';
answer.style.color = 'rgb(8, 134, 8)';
incrementScore();
} else {
answer.innerText = 'Errou! Tente novamente!';
}
}
}
function startGame() {
const hitedElement = document.getElementsByClassName('hitedElement')[0];
if (hitedElement) {
hitedElement.classList.remove('hitedElement');
}
answer.innerText = 'Escolha uma cor';
answer.style.color = 'black';
const specificColor = generateRandomColor();
applySpecificColor(specificColor);
applyRandomColors(specificColor);
for (let index = 0; index < circles.length; index += 1) {
circles[index].addEventListener('click', (event) => {
hitTheColor(event, specificColor);
});
}
}
startGame();
const resetButton = document.getElementById('reset-game');
resetButton.addEventListener('click', () => {
startGame();
});