-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
102 lines (84 loc) · 2.32 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
let gameSeq = [];
let userSeq = [];
let scores = [];
let btnList = ["orange", "pink", "blue", "green"];
let level = 0;
let started = false;
let h2 = document.querySelector("h2");
document.addEventListener("keypress", function() {
if (started == false) {
started = true;
console.log("started");
gameLevelUp();
}
});
function gameLevelUp() {
userSeq = [];
level++;
h2.innerText = `Level ${level}`;
let randIndex = Math.floor(Math.random() * 4);
let randColor = btnList[randIndex];
let randButton = document.querySelector(`.${randColor}`);
gameSeq.push(randColor);
console.log(gameSeq);
buttonFlash(randButton);
}
function buttonFlash(btn) {
btn.classList.add("flash");
setTimeout(function() {
btn.classList.remove("flash")
}, 150);
}
function matchColors(index) {
if (userSeq[index] === gameSeq[index]) {
if (userSeq.length == gameSeq.length) {
setTimeout(gameLevelUp, 1000);
}
} else {
if (level == 0) {
scores.push(level);
} else {
scores.push(level-1);
}
let highScoreVal = maxScore(scores);
if (level == 0) {
h2.innerHTML = `Oops! You clicked before game started!<br><br>Press any key to restart the game!`;
} else {
h2.innerHTML = `Game Over! Your score was ${level-1}.
<br>Press any key to restart the game!<br><br><span>HIGH SCORE : ${highScoreVal}</span>`;
}
let body = document.querySelector("body");
body.style.backgroundColor = "#c9184a";
setTimeout(function() {
body.style.backgroundColor = "#e2ece9";
},200);
gameReset();
}
}
function maxScore(scores) {
let max = scores[0];
for (let i=0; i<scores.length; i++) {
if (scores[i]>max) {
max = scores[i];
}
}
return max;
}
function userButton() {
let btn = this;
// console.log(btn);
buttonFlash(btn);
let userColor = btn.getAttribute("id");
userSeq.push(userColor);
matchColors(userSeq.length-1);
}
let allBtns = document.querySelectorAll(".btn");
for (btn of allBtns) {
btn.addEventListener("click", userButton);
}
function gameReset() {
started = false;
gameSeq = [];
userSeq = [];
level = 0;
}