-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.js
177 lines (153 loc) · 3.55 KB
/
memory.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const cardarray = [
{
name : 'fries',
img : 'fries.png'
},
{
name : 'cheeseburger',
img : 'cheeseburger.png'
},
{
name : 'hotdog',
img : 'hotdog.png'
},
{
name : 'ice-cream',
img : 'ice-cream.png'
},
{
name : 'milkshake',
img : 'milkshake.png'
},
{
name : 'pizza',
img : 'pizza.png'
},
{
name : 'fries',
img : 'fries.png'
},
{
name : 'cheeseburger',
img : 'cheeseburger.png'
},
{
name : 'hotdog',
img : 'hotdog.png'
},
{
name : 'ice-cream',
img : 'ice-cream.png'
},
{
name : 'milkshake',
img : 'milkshake.png'
},
{
name : 'pizza',
img : 'pizza.png'
}
]
const h4 = document.querySelector('h4');
var w = document.querySelector('#q');
var c= 50;
w.innerText = c;
var int = setInterval(() => {
w.innerText=c;
if (c<=0)
{
w.innerText='0';
clearInterval(int);
h4.innerText='You Lost!';
alert('YOU LOST');
removeBoard();
}
c--;
}, 1000);
var score =0;
var cardsChosen =[];
var cardschosenid = [];
console.log(cardarray);
cardarray.sort(() => 0.5 -Math.random());
const gridDisplay = document.querySelector('.b');
function checkmatch(){
const cards = document.querySelectorAll('.b img');
const h1 = document.querySelector('h1');
const h2 = document.querySelector('h2');
const h3 = document.querySelector('h3');
const h4 = document.querySelector('h4');
let a = cardschosenid[0];
let y = cardschosenid[1];
if (a===y)
{
alert('Dont click the same image');
cards[cardschosenid[0]].setAttribute('src','blank.png');
}
else if (cardsChosen[0]===cardsChosen[1])
{
cards[cardschosenid[0]].setAttribute('src','white.png');
cards[cardschosenid[1]].setAttribute('src','white.png');
cards[cardschosenid[0]].removeEventListener('click',flipcard);
cards[cardschosenid[1]].removeEventListener('click',flipcard);
score++;
h2.innerText=`Your score is ${score}`;
if (score>=6)
{
alert('Congrats you won!');
h3.innerText='You Won!';
clearInterval(int);
but.style.visibility = 'visible';
}
else if (score<6 && c===0)
{
removeBoard();
}
}
else
{
cards[cardschosenid[0]].setAttribute('src','blank.png');
cards[cardschosenid[1]].setAttribute('src','blank.png');
cardsChosen =[];
cardschosenid = [];
}
cardsChosen = [];
cardschosenid = [];
}
function removeBoard() {
const inp = document.querySelectorAll('img');
try{
if (inp[0].src===null)
{
}
else
{
for (let i=0;i<12;i++)
{
inp[i].remove();
}
}
}
catch {
}
}
function createBoard() {
for (let i=0;i<12;i++)
{
const img = document.createElement('img');
img.src='blank.png';
img.setAttribute('data-id',i);
img.addEventListener('click',flipcard);
gridDisplay.append(img);
}
}
createBoard();
function flipcard() {
let q = this.getAttribute('data-id');
this.setAttribute('src',cardarray[q].img);
cardsChosen.push(cardarray[q].name);
cardschosenid.push(q)
if (cardsChosen.length ===2)
{
setTimeout(checkmatch,500);
}
}