-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
74 lines (65 loc) · 1.78 KB
/
app.js
File metadata and controls
74 lines (65 loc) · 1.78 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
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
let gameSeq=[];
let userSeq=[];
let btnlist=["first","second","third","fourth"];
let started=false;
let h3=document.querySelector('h3');
document.addEventListener("keypress",function(){
if(started==false){
started=true;
game();
}
});
let level=0;
function game(){
level++;
userSeq=[];
h3.innerText=`LEVEL ${level}`;
let btnidx=Math.floor(Math.random()*3);
let btn=document.querySelector(`.${btnlist[btnidx]}`);
gameSeq.push(btnlist[btnidx]);
gameflash(btn);
}
function gameflash(btn){
btn.classList.add("gameflash");
setTimeout(function(){
btn.classList.remove("gameflash");
},200);
}
function userflash(btn){
btn.classList.add("userflash");
setTimeout(function(){
btn.classList.remove("userflash");
},200);
}
function btnclick(){
let btn=document.getElementById(this.id);
userSeq.push(`${this.id}`);
setTimeout(userflash(btn),3000);
checkans();
}
function checkans(){
let idx=userSeq.length-1;
if(gameSeq[idx]!=userSeq[idx]){
h3.innerText=" WRONG MOVE GAME OVER!!";
document.querySelector("body").style.backgroundColor="red";
setTimeout(function(){
document.querySelector("body").style.backgroundColor="white";
h3.innerHTML=`YOUR SCORE IS <b>${level} </b> <br> ENTER ANY KEY TO RESTART THE GAME`;
reset();
},1000);
//
}
else if(gameSeq.length==userSeq.length){
setTimeout(game,1000);
}
}
let btns=document.querySelectorAll('.boxes');
for(let btn of btns){
btn.addEventListener("click",btnclick);
}
function reset(){
gameSeq=[];
userSeq=[];
level=0;
started=false;
}