-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
107 lines (73 loc) · 1.85 KB
/
script.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
let buttons=document.querySelectorAll(".box");
let h=document.querySelector("h3");
let body=document.querySelector("body");
let box=["one","two","three","four"];
let arr=[];
let input=[];
let i = 0;
let level=0;
let start=false;
body.addEventListener('keydown',function(){
let i = 0;
if(start==false){
document.getElementsByClassName("over").innerText = ""
start=true;
arr=[];
input=[];
generatearr();
// check();
}
});
// input :- 1 -> 1, 3 -> 1, 3 ,4
// input arr = [1, 1, 3, 1, 3 ,4 ]
function generatearr(){
level++;
h.innerText=`level ${level}`;
let idx=Math.floor(Math.random()*3);
arr.push(box[idx]);
// console.log(box[])
input = [];
i = 0;
flash(buttons[idx]);
}
for(bt of buttons){
bt.addEventListener("click",function(event){
input.push(event.target.id);
console.log(event.target.id);
flash(this);
//console.log(arr, input)
if(arr[i] != input[i]){
start = false;
h.innerText = ` your score is ${level} `;
document.querySelector(".over").innerText = "Game Over";
}
if(input.length==arr.length){
check();
}
i++;
});
}
function flash(btn){
btn.classList.add("white");
setTimeout(function(){
btn.classList.remove("white");
},200);
}
function check(){
var check = true
for(let i=0;i<arr.length;i++){
if(arr[i]!=input[i]){
check=false;
break;
}
}
if(check){
setTimeout(() => {
generatearr();
}, 900);
}else{ // todo if failed
start = false;
h.innerText = `${level} is your score...`;
document.querySelector(".over").innerText = "Game Over"
}
}