-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
66 lines (63 loc) · 2.12 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
let sequence = [];
let user = [];
let h2 = document.querySelector("h2");
let div = document.querySelector(".play");
let score=document.querySelector("p");
let level=0;
let high =0;
div.addEventListener("click", function (event) {
if (sequence.length == 0&&level==0) {
let a = Math.floor(Math.random() * 4 + 1);
console.log(a);
sequence.push(a);
let temp = document.getElementById(`${a}`);
let color = temp.style.backgroundColor;
temp.style.backgroundColor = "white";
h2.innerText = "Level : 1";
level++;
setTimeout(function () {
temp.style.backgroundColor = color;
}, 200);
}
else if(sequence.length!=0){
let idx=user.length;
user.push(Number(event.target.id));
let temp = event.target;
let color = temp.style.backgroundColor;
temp.style.backgroundColor = "white";
setTimeout(function () {
temp.style.backgroundColor = color;
}, 200);
if(user[idx]!=sequence[idx]){
h2.innerHTML = `GAME OVER !! Your Score : ${level}<br> To restart click any color`;
high=Math.max(high,level);
score.innerText=`HIGHEST SCORE : ${high} `;
let body=document.querySelector(".container");
body.style.backgroundColor="red";
setTimeout(function () {
body.style.backgroundColor = "white";
}, 200);
sequence=[];
user=[];
level=0;
}
else if(user.length==sequence.length){
level++;
h2.innerText = `Level : ${level}`;
user=[];
setTimeout(function (){
levelup();
},1000);
}
}
});
function levelup(){
let a = Math.floor(Math.random() * 4 + 1);
sequence.push(a);
let temp = document.getElementById(`${a}`);
let color = temp.style.backgroundColor;
temp.style.backgroundColor = "white";
setTimeout(function () {
temp.style.backgroundColor = color;
}, 200);
}