-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (58 loc) · 1.31 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
let gamesqn=[];
let usersn=[];
let highest=0;
let start=false;
let level=0;
let buttons=["btn1","btn2","btn3","btn4"];
let h2=document.querySelector("h2");
document.addEventListener("keypress",function(){
if(start==false){
start=true;
levelup();
}
})
function flashbtn(button){
button.classList.add("flash");
setTimeout(function(){
button.classList.remove("flash");
} ,200);
}
function levelup(){
usersn=[];
level++;
h2.innerText=`LEVEL ${level}`;
let randInd=Math.floor(Math.random()*3);
let randColor=buttons[randInd];
let randBtn=document.querySelector(`.${randColor}`);
gamesqn.push(randColor);
flashbtn(randBtn);
}
function checkAns(idx){
if(usersn[idx]==gamesqn[idx]){
if(usersn.length==gamesqn.length){
setTimeout(levelup,1000);
}
}
else{
highest=Math.max(level,highest);
h2.innerText=`wrong key pressed \t your score is ${level} !!\n\npress any key to start again \n heighest score : ${highest}`;
reset();
}
}
function btnPress(){
let btn=this;
flashbtn(btn);
let color=btn.getAttribute("id");
usersn.push(color);
checkAns(usersn.length-1);
}
let allBtns=document.querySelectorAll(".button");
for(btn of allBtns){
btn.addEventListener("click",btnPress);
}
function reset(){
start=false;
gamesqn=[];
usersn=[];
level=0;
}