-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
242 lines (157 loc) · 5.36 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const gameContainer = document.getElementById("game");
const COLORS = [
"red",
"blue",
"green",
"orange",
"purple",
"red",
"blue",
"green",
"orange",
"purple"
];
// here is a helper function to shuffle an array
// it returns the same array with values shuffled
// it is based on an algorithm called Fisher Yates if you want to research more
function shuffle(array) {
let counter = array.length;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
let index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
let temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
let shuffledColors = shuffle(COLORS);
// this function loops over the array of colors
// it creates a new div and gives it a class with the value of the color
// it also adds an event listener for a click for each card
function createDivsForColors(colorArray) {
// for (let color of colorArray) {
for(let i = 0; i<colorArray.length; i++){
const newDiv = document.createElement("div");
// give it a class attribute for the value we are looping over
newDiv.classList.add(colorArray[i]);
//set the color
//newDiv.style.backgroundColor = colorArray[i];
newDiv.setAttribute('id', i);
newDiv.setAttribute('background-color', 'black');
// call a function handleCardClick when a div is clicked on
newDiv.addEventListener("click", handleCardClick);
// append the div to the element with an id of game
gameContainer.append(newDiv);
}
// // create a new div
// const newDiv = document.createElement("div");
// // give it a class attribute for the value we are looping over
// newDiv.classList.add(color);
// newDiv.style.backgroundColor = color;
// // call a function handleCardClick when a div is clicked on
// newDiv.addEventListener("click", handleCardClick);
// // append the div to the element with an id of game
// gameContainer.append(newDiv);
// }
}
let lastColor = '';
let lastElementId = '';
let numClicked = 0;
let numMatches = 0;
//numMatches is an unused variable, could display if wanted
let matchedArray = [];
// TODO: Implement this function!
function handleCardClick(event) {
// you can use event.target to see which element was clicked
//change the color when clicked
//do nothing/stop the function if the card is already flipped
// if (event.target.classList.contains("flipped")){
// return;
// }
event.target.classList.add("flipped");
//console.log(event.target.classList[0]);
numClicked++;
console.log(event.target.classList[0]);
//check if the element clicked has the same color AND a different ID than the last clicked element
if((event.target.classList[0]===lastColor)&& event.target.id!==lastElementId && numClicked==2){
//logic to count as a match
numMatches++;
matchedArray.push(event.target.id);
matchedArray.push(lastElementId);
}
//console.log(event.target.parentElement.childNodes);
for (char of event.target.parentElement.childNodes){
// console.log(char);
// console.log(event.target.parentElement.childNodes);
//console.log(char);
if(char.nodeName != "#text"){
for(arrayChar of matchedArray){
//console.log(char.id);
//console.log("arrayChar",arrayChar);
//console.log(char.classList);
if(char.id === arrayChar){
char.classList.add("matched");
}
// else if(numClicked<2){
// if(char.nodeName != "#text"){
// char.classList.remove("flipped");
// }
// }
}
}
console.log("char",char);
if(char.nodeName != "#text"){
if(char.classList.contains("matched") || char.classList.contains("flipped")){
char.style.backgroundColor = char.classList[0];
}
}
}
//for loop end--------------------------------------------------------
if(numClicked===1){
lastElementId = event.target.id;
//console.log(event.target.classList[0]);
lastColor = event.target.classList[0];
}
else if (numClicked>=2){
event.target.style.backgroundColor = event.target.classList[0];
//logic to reflip the last two cards
//event.target.style.backgroundColor = 'black';
//clear lastElementId
setTimeout(
function()
{for (char of event.target.parentElement.childNodes){
// console.log(char);
// console.log(event.target.parentElement.childNodes);
//console.log(char);
if(char.nodeName != "#text"&& !char.classList.contains("matched")){
char.classList.remove("flipped");
char.style.backgroundColor = "white"
lastElementId = '';
//clear lastColor
lastColor = '';
numClicked = 0;
}}}
, 1000);
// lastElementId = event.target.id;
//console.log(event.target.classList[0]);
// lastColor = event.target.classList[0];
}
// console.log(event.target.parentElement);
//add the flipped class to anything that's been matched
// else(){
// event.target.classList.remove("flipped");
// }
// console.log("numMatches",numMatches);
// console.log("lastElementID",lastElementId);
// console.log("matchedArray",matchedArray);
//set the last clicked to this
//event.parent
}
// when the DOM loads
createDivsForColors(shuffledColors);
/* */