This repository was archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmemory-card.js
More file actions
61 lines (54 loc) · 1.52 KB
/
memory-card.js
File metadata and controls
61 lines (54 loc) · 1.52 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
const cards = document.querySelectorAll('.memory-card')
let hasFlipCard = false
let lockBord = false
let firstCard, secondCard
function flipCard() {
if (lockBord) return //if lockboard is true
// console.log(this);
if (this === firstCard) return
this.classList.toggle('flip')
//classList.toggle => acess the classlist of the memory card and toggle => if the class is there remove it if its not add it
if (!hasFlipCard) {
//if hasflipcard is false
//first time clicked card
hasFlipCard = true
firstCard = this
//console.log(hasFlipCard, firstCard);
return
}
secondCard = this
checkMatch()
}
//imediate invoke function
(function shuffle() {
cards.forEach(card => {
//eslint-disable-next-line
let randomPs = Math.floor(Math.random() * 12);
// @ts-ignore
card.style.order = randomPs
})
})()
function resetBord() {
[hasFlipCard, lockBord] = [false, false];
[firstCard, secondCard] = [null, null]
}
function checkMatch() {
//eslint-disable-next-line
let isMatch = firstCard.dataset.framework === secondCard.dataset.framework;
//eslint-disable-next-line
isMatch ? disableCards() : unflipCards();
}
function disableCards() {
firstCard.removeEventListener('click', flipCard)
secondCard.removeEventListener('click', flipCard)
resetBord()
}
function unflipCards() {
lockBord = true
setTimeout(() => {
firstCard.classList.remove('flip')
secondCard.classList.remove('flip')
resetBord()
}, 1500)
}
cards.forEach(card => card.addEventListener('click', flipCard))