-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava.js
84 lines (69 loc) · 1.69 KB
/
java.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
let start =true;
let level=0;
let h2=document.querySelector("h2");
let btns=document.querySelectorAll(".btn");
let maxscr=document.querySelector(".max");
let color=["green","red","yellow","purple"];
let gameSeq=[];
let mySeq=[]
let maxScore =0;
// gameSeq.push("blue");
// console.log(gameSeq);
document.addEventListener("keypress",function(){
if(start==true){
start=false;
levelUp();
}
});
function levelUp(){
level++;
// console.log(level);
h2.innerText=`Level ${level}`
let rIdx=Math.floor(Math.random()*3);
let clr=color[rIdx];
gameSeq.push(clr);
console.log(gameSeq);
flashGame(clr)
}
function flashGame(clr){
// console.log(clr);
document.querySelector(`.${clr}`).classList.add("flash")
setTimeout(function(){
document.querySelector(`.${clr}`).classList.remove("flash");
},300);
}
function flashUser(clr){
document.querySelector(`.${clr}`).classList.add("user")
setTimeout(function(){
document.querySelector(`.${clr}`).classList.remove("user");
},200);
}
for(btn of btns){
btn.addEventListener("click",addUserSeq);
}
function addUserSeq(){
let clr=this.getAttribute("id");
mySeq.push(clr);
flashUser(clr);
check();
}
function check(){
let idx=mySeq.length-1;
// console.log(mySeq)
if(mySeq[idx]==gameSeq[idx] && idx==gameSeq.length-1){
mySeq=[];
levelUp();
}else if(mySeq[idx]!=gameSeq[idx]){
h2.innerHTML=`You Lose game your score is <b> ${gameSeq.length} </b> <br> press any key to restart `;
maxScore=Math.max(maxScore,gameSeq.length);
maxscr.innerText=`MAX SCORE IS ${maxScore}`;
reset();
}
}
function reset(){
gameSeq=[];
mySeq=[];
start=true;
level=0;
// h2.innerText
}