-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
212 lines (151 loc) · 5.01 KB
/
app.js
File metadata and controls
212 lines (151 loc) · 5.01 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const arr = [
{
name: 'Dwight',
img: "images/Dwight.jpeg",
},
{
name: 'Dwight',
img: "images/Dwight.jpeg",
},
{
name: "Ipod",
img: 'images/Ipod.jpeg',
},
{
name: "Ipod",
img: 'images/Ipod.jpeg',
},
{
name: 'ItsBrittanyB',
img: 'images/ItsBrittanyB.jpeg',
},
{
name: 'ItsBrittanyB',
img: 'images/ItsBrittanyB.jpeg',
},
{
name: 'Jim',
img: 'images/Jim.jpeg',
},
{
name: 'Jim',
img: 'images/Jim.jpeg',
},
{
name: 'PrisonMike',
img: 'images/PrisonMike.jpg',
},
{
name: 'PrisonMike',
img: 'images/PrisonMike.jpg',
},
{
name: 'Stanley',
img: 'images/Stanley.jpeg'
},
{
name: 'Stanley',
img: 'images/Stanley.jpeg'
},
]
const grid = document.querySelector('.grid')
const timer = document.querySelector('#timer')
const startBtn = document.querySelector('#start-button')
const resetBtn = document.querySelector('#reset')
const score = document.querySelector('#score')
const won = document.querySelector('.youWon')
let clicked = []
let clickedNum = []
let matched = []
let time = 60
let test
function shuffleCards(){
let newCard
let tempCard
for(let i = arr.length - 1; i >= 0; i--){
newCard = Math.floor(Math.random() * 11)
tempCard = arr[i]
arr[i] = arr[newCard]
arr[newCard] = tempCard
}
return arr
}
//Timer
function timeCountDown(){
test = setInterval(function(){
if(time <= 0){
clearInterval(time = 0)
return timeOut()
}
timer.innerHTML = `${time} seconds`
time -= 1
}, 1000)
}
function timeOut(){
document.querySelector('#timer').textContent= 'Time Out'
document.querySelector('.grid').innerHTML = " "
document.querySelector('.timeOut').textContent = "TIME OUT!!!"
document.querySelector('.youWon').textContent = `You Found: ${matched.length} Matches`
}
startBtn.addEventListener('click', timeCountDown)
//create the board
function gameBoard(){
for(let i = 0; i < arr.length; i++){
let card = document.createElement('img')
card.setAttribute('class', 'images')
card.setAttribute('src', 'images/TheOffice.jpeg')
card.setAttribute('data-index', i)
card.addEventListener('click', cardImgDisplay)
grid.appendChild(card)
}
shuffleCards()
}
gameBoard()
//check to see if the cards match
function checkCard(){
let cards = document.querySelectorAll('img')
let cardNum1 = clickedNum[0]
let cardNum2 = clickedNum[1]
if (clicked[0] === clicked[1]){
cards[cardNum1].removeEventListener('click', cardImgDisplay)
cards[cardNum2].removeEventListener('click', cardImgDisplay)
matched.push(clicked)
} else {
cards[cardNum1].setAttribute('src','images/TheOffice.jpeg')
cards[cardNum2].setAttribute('src','images/TheOffice.jpeg')
}
clicked = []
clickedNum = []
score.textContent = matched.length
if(matched.length === arr.length/2){
won.textContent = 'You Found All 6 Matches!!!'
score.textContent = 'You won!!!'
}
}
//flip cards
function cardImgDisplay(){
let cardImgNum = this.getAttribute('data-index')
this.setAttribute('src', arr[cardImgNum].img)
clicked.push(arr[cardImgNum].name)
clickedNum.push(cardImgNum)
// this.setAttribute('src', arr[cardImgNum].img)
if(clicked.length === 2){
setTimeout(() => checkCard(), 500)
}
}
resetBtn.addEventListener('click', reset)
function reset(){
time = 60
clearInterval(test)
startBtn.removeEventListener('click', timeCountDown)
startBtn.addEventListener('click', timeCountDown)
clicked = []
clickedNum = []
matched = []
document.querySelector('.grid').innerHTML = " "
document.querySelector('.timeOut').textContent = " "
document.querySelector('#score').textContent = " "
document.querySelector('.youWon').textContent = " "
document.querySelector('#timer').innerHTML = `${time} seconds`
gameBoard()
}