-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (52 loc) · 1.89 KB
/
index.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
//Selects all HTML elements with the class card
const cards = document.querySelectorAll(".card");
//Variables
let flippedCard = false; //Boolean value for card flipping
let locked = false; //Boolean value to lock and unlock board
let firstCard, secondCard; //Two variables for card pair
function flipCard() {
//If the board is locked, return (rest of code will not be executed)
if (locked) return;
//If user clicks the same card twice, return (rest of code will not be executed)
if (this === firstCard) return;
this.classList.add("flip");
if (!flippedCard) {
// If user has clicked on card, set flippedCard to true
// and firstCard to reference the clicked card
flippedCard = true;
firstCard = this;
} else {
// second click
flippedCard = false;
secondCard = this;
//If the first card matches the second card -> remove event listener
// dataset takes the name of the element given in data-* (data-pokemon in HTML file)
if (firstCard.dataset.pokemon === secondCard.dataset.pokemon) {
firstCard.removeEventListener("click", flipCard);
secondCard.removeEventListener("click", flipCard);
resetBoard();
} else {
locked = true;
//Cards do not match, remove class and reset the board
setTimeout(() => {
firstCard.classList.remove("flip");
secondCard.classList.remove("flip");
resetBoard();
}, 1500);
}
}
}
//Set default values again
function resetBoard() {
[flippedCard, locked] = [false, false];
[firstCard, secondCard] = [null, null];
}
//Runs on declaration-> shuffles cards on the board
(function shuffle() {
cards.forEach((card) => {
let random = Math.floor(Math.random() * 12);
card.style.order = random;
});
})();
//For each card, add an event listener that checks whether the card has been clicked
cards.forEach((card) => card.addEventListener("click", flipCard));