-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
82 lines (69 loc) · 1.76 KB
/
app.js
File metadata and controls
82 lines (69 loc) · 1.76 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
75
76
77
78
79
80
81
82
let gameSeq = [];
let userSeq = [];
let started = false;
let level = 0;
let heading3=document.querySelector("h3");
let btns=["first","second","third","fourth"];
document.addEventListener("keypress",function(){
if(started==false){
console.log("game started");
started=true;
levelup();
}
})
function btnflash(btn){
btn.classList.add("flash");
setTimeout(function(){
btn.classList.remove("flash");
},225);
}
function levelup(){
userSeq=[];
level++;
heading3.innerText=`level ${level}`;
//random color
let randIdx=Math.floor(Math.random()*3);
let randcol=btns[randIdx];
let randbtn=document.querySelector(`.${randcol}`)
// console.log(randIdx);
// console.log(randcol);
// console.log(randbtn);
gameSeq.push(randcol);
console.log(gameSeq);
btnflash(randbtn);
}
function checkans(idx){
// let idx=level-1;
if(gameSeq[idx]==userSeq[idx]){
if(userSeq.length==gameSeq.length){
setTimeout(levelup,1000);
}
}
else{
heading3.innerHTML=`Game Over! your score was <b>${level}</b><br> press any key to restart`;
document.querySelector("body").style.backgroundColor="red";
setTimeout(function(){
document.querySelector("body").style.backgroundColor="white";
},150);
reset();
}
}
function btnpress(){
// console.log("button pressed");
let btn=this;
btnflash(btn);
let usercolor=btn.getAttribute("id");
//console.log(usercolor);
userSeq.push(usercolor);
checkans(userSeq.length-1);
}
let butp=document.querySelectorAll(".btn");
for(buts of butp){
buts.addEventListener("click",btnpress);
}
function reset(){
started = false;
level = 0;
gameSeq=[];
userSeq=[];
}