-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefactortest.js
More file actions
58 lines (49 loc) · 2.08 KB
/
refactortest.js
File metadata and controls
58 lines (49 loc) · 2.08 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
const card = document.querySelectorAll(".card");
let userFlippedCard = false;
let firstSelection = "";
let secondSelection = "";
// Step 3: Put it in a function. A.K.A function declaration
function cardFlip() {
// create and access class list of "cards" and toggle "flip" class
// Definition: Using classList is a convenient alternative to accessing an element's list of classes as a space-delimited string via element.className.
this.classList.add('flip');
// console.log to confirm that "flip" is attached to "card" on click? this will confirm it flipped?
if (!userFlippedCard){
// for user 1st click
userFlippedCard = true;
firstSelection = this;
// console.log({userFlippedCard, firstSelection}); ==> to test if userFlippedCrad = true
} else {
// for user 2nd click
userFlippedCard = false;
secondSelection = this;
// do cards match
if (firstSelection.dataset.logo ===
secondSelection.dataset.logo){
firstSelection.removeEventListener("click", cardFlip);
secondSelection.removeEventListener("click", cardFlip);
} else {
// not a match
setTimeout(() => {
firstSelection.classList.remove("flip");
secondSelection.classList.remove("flip");
resetBoard()
}, 1500);
}
}
}
function checkForMatch(){
if(firstSelection.dataset.logo === secondSelection.dataset.logo){
firstSelection.removeEventListener("click". cardFlip);
secondSelection.removeEventListener("click", cardFlip);
} else {
setTimeout (() => {
firstSelection.classList.remove("flip");
secondSelection.classList.remove("flip");
resetBoard()
}, 1500);
}
}
card.forEach(card => { // Step 2: loop through the list of card elements into each card (using for each loop)
card.addEventListener("click", cardFlip); // adds event listener (click event) that executes a function "cardFlip" when fired
});