-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
48 lines (37 loc) · 1.22 KB
/
script.js
File metadata and controls
48 lines (37 loc) · 1.22 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
const balls = document.querySelectorAll('.ball');
const circles = document.querySelector('#circles');
const rgbOrigem = document.querySelector('#rgb-color');
const resposta = document.querySelector('#answer');
const btnReset = document.querySelector('#reset-game');
const txtPlacar = document.querySelector('#score');
let placar = 0;
circles.addEventListener('click',comparaCor);
btnReset.addEventListener('click',reload);
function criaBalls(){
for (index of balls){
index.style.backgroundColor=rgbAleatorio();
txtPlacar.innerText = placar;
rgbOrigem.innerText = balls[ballAleatorio()].style.backgroundColor;
resposta.innerText = 'Escolha uma cor';
}
}
criaBalls();
function reload(){
criaBalls();
}
function rgbAleatorio(){
let mix = [Math.floor(Math.random() * 256), Math.floor(Math.random() * 256), Math.floor(Math.random() * 256)];
return "rgb(" + mix + ")";
}
function ballAleatorio(){
return Math.floor(Math.random() * balls.length);
}
function comparaCor(origin){
if (origin.target.style.backgroundColor == rgbOrigem.innerText){
resposta.innerText = 'Acertou!';
placar = placar + 3;
txtPlacar.innerText = placar;
} else {
resposta.innerText = 'Errou! Tente novamente!';
}
}