-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
103 lines (86 loc) · 2.33 KB
/
app.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
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
let body = document.querySelector('body');
let h3 = document.querySelector('h3');
let gameseq = [];
let userseq = [];
let started = false;
let level = 0;
let btn1 = document.querySelector('#a');
let btn2 = document.querySelector('#b');
let btn3 = document.querySelector('#c');
let btn4 = document.querySelector('#d');
function random() {
let x = (Math.floor(Math.random() * 10 / 2.5)) + 1;
return x;
}
body.addEventListener('click', function () {
if (started == false) {
h3.innerText = 'Game Started';
started = true;
push1();
}
})
function resetGame() {
gameseq = [];
userseq = [];
started = false;
h3.innerText = 'Game Over, Click to Restart';
}
function push1() {
h3.innerText = `Level ${gameseq.length}`
let randomx = random();
gameseq.push(randomx);
console.log(...gameseq);
let selectedButton;
if (randomx === 1) selectedButton = btn1;
else if (randomx === 2) selectedButton = btn2;
else if (randomx === 3) selectedButton = btn3;
else if (randomx === 4) selectedButton = btn4;
selectedButton.classList.add('flash');
setTimeout(function () {
selectedButton.classList.remove('flash');
}, 300);
}
btn1.addEventListener('click', function () {
userseq.push(1);
if (userseq[userseq.length - 1] != gameseq[userseq.length - 1]) {
alert("Game ended");
resetGame();
}
else if (userseq.length === gameseq.length) {
userseq = [];
push1();
}
})
btn2.addEventListener('click', function () {
userseq.push(2);
if (userseq[userseq.length - 1] != gameseq[userseq.length - 1]) {
alert("Game ended");
resetGame();
}
else if (userseq.length === gameseq.length) {
userseq = [];
push1();
}
})
btn3.addEventListener('click', function () {
userseq.push(3);
if (userseq[userseq.length - 1] != gameseq[userseq.length - 1]) {
alert("Game ended");
resetGame();
}
else if (userseq.length === gameseq.length) {
userseq = [];
push1();
}
})
btn4.addEventListener('click', function () {
userseq.push(4);
if (userseq[userseq.length - 1] != gameseq[userseq.length - 1]) {
alert("Game ended");
resetGame();
}
else if (userseq.length === gameseq.length) {
userseq = [];
push1();
}
})