-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
71 lines (64 loc) · 1.8 KB
/
script.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
window.addEventListener("load", (e) => {
const btns = document.querySelector(".btns");
const btn1 = document.querySelector(".btn1");
const btn2 = document.querySelector(".btn2");
const btn3 = document.querySelector(".btn3");
const btn0 = document.querySelector(".btn0");
const points = document.querySelector(".points");
let colors = ["red", "green", "purple", "yellow"];
let orderArr = [];
let arr = [];
let score = 0;
let j = 0;
function reset() {
btn1.style.backgroundColor = "#555555";
btn2.style.backgroundColor = "#555555";
btn3.style.backgroundColor = "#555555";
btn0.style.backgroundColor = "#555555";
j = 0;
orderArr = [];
}
function check() {
if (orderArr.length == arr.length) {
if (JSON.stringify(orderArr) == JSON.stringify(arr)) {
score = score + 1;
} else {
score = score - 1;
}
points.innerHTML = `${score}`;
reset();
}
}
function start() {
reset();
arr = [];
let i = 0;
while (arr.length != 4) {
let a = Math.floor(Math.random() * 4);
if (!arr.includes(a.toString())) arr.push(a.toString());
}
const time = setInterval((e) => {
document.querySelector(
`.btn${arr[i]}`
).style.backgroundColor = `${colors[i]}`;
i++;
if (i == 4) clearTimeout(time);
}, 500);
setTimeout(() => {
reset();
}, 3000);
console.log(arr);
}
reset();
document.addEventListener("keypress", (e) => {
if (e.key == "s" || "S") start();
if (e.key == "r" || "R") reset();
});
btns.addEventListener("click", (e) => {
orderArr.push(e.target.id);
e.target.style.backgroundColor = `${colors[j]}`;
j++;
console.log(orderArr);
check();
});
});