-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
192 lines (154 loc) · 5.51 KB
/
script.js
File metadata and controls
192 lines (154 loc) · 5.51 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
const gameContainer = document.getElementById("game");
let compareCardCount = 0;
let matchedPairsCount = 0;
let currentFlippedCardsArr = [];
let matchedCardsArr = [];
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 ot 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) {
// 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);
// 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);
}
}
// TODO: Implement this function!
function isMatch(cardOne, cardTwo) {
if (cardOne == cardTwo) {
matchedCardsArr.push(cardOne);
matchedCardsArr.push(cardTwo);
console.log(matchedCardsArr);
return true;
} else {
return false;
}
}
function isGameOver() {
if (matchedPairsCount = COLORS.length / 2) {
return true;
} else {
return false;
}
}
let firstFlippedCard = "";
let secondFlippedCard = "";
function handleCardClick(event) {
event.preventDefault();
let clickedCard = event.target;
compareCardCount++;
if (!(compareCardCount > 2)) {
if (compareCardCount === 1) {
firstFlippedCard = clickedCard;
firstFlippedCard.style.backgroundColor = clickedCard.className;
firstFlippedCard.classList.add('isFlipped');
} else {
secondFlippedCard = clickedCard;
secondFlippedCard.style.backgroundColor = clickedCard.className;
secondFlippedCard.classList.add('isFlipped');
compareCardCount = 0;
if (firstFlippedCard.className == secondFlippedCard.className) {
matchedPairsCount++;
const matchedCards = document.querySelectorAll(".isFlipped");
console.log(matchedCards);
for (let card of matchedCards) {
card.classList.add('isMatched');
card.classList.remove('isFlipped');
}
console.log(matchedPairsCount);
matchedCardsArr.push(firstFlippedCard);
matchedCardsArr.push(secondFlippedCard);
console.log(matchedCardsArr);
} else {
setTimeout(function(){
const unMatchedCards = document.querySelectorAll(".isFlipped");
console.log(unMatchedCards);
for(let card of unMatchedCards) {
card.style.backgroundColor = "white";
card.classList.remove('isFlipped');
}
//secondFlippedCard.classList.remove('isFlipped');
}, 2000);
}
}
} else {
alert("You've already clicked two cards!");
}
//getAttribute: isFlipped.....
}
// when the DOM loads
createDivsForColors(shuffledColors);
// function handleCardClick(event) {
// event.preventDefault();
// let clickedCard = event.target;
// let flipped = {};
// ++compareCardCount;
// currentFlippedCardsArr.push(flipped);
// //getAttribute: isFlipped.....
// if (!(compareCardCount > 2 || flipped.isFlipped)) {
// if (currentFlippedCardsArr.length === 1 || currentFlippedCardsArr.length === 2) {
// flipped.clickedCardColor = event.target.className;
// flipped.isFlipped = true;
// clickedCard.style.backgroundColor = flipped.clickedCardColor;
// console.log('compareCardCount: ' + compareCardCount);
// //console.log("you just clicked", event.target);
// console.log(currentFlippedCardsArr);
// console.log(flipped);
// }
// if (currentFlippedCardsArr.length === 2 && currentFlippedCardsArr[0].clickedCardColor != currentFlippedCardsArr[1].clickedCardColor) {
// setTimeout(() => {
// console.log("clear the area and change card back");
// //for each element in the currentFlippedCardsArr, remove them
// // flipped.clickedCardColor = " ";
// // flipped.isFlipped = false;
// // currentFlippedCardsArr = [];
// }, 2000)
// }
// else {
// console.log("we may have a match");
// }
// }
// else {
// console.log("You have already flipped 2 cards! or Card is already flipped.");
// }
// }
// // when the DOM loads
// createDivsForColors(shuffledColors);
/*click a card and reveal its color if the target class === a certain color in the color array make the card that color -- click another card to do the same-- if the cards don't match, turn both cards back to their initial state -- if the cards do match, keep them turned*/
/**a counter variable for how many cards are flipped -- a pair variable to keep track of mathcing pairs -- setting a timer for how long the cards are flipped -- think about session storage**/