-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
56 lines (46 loc) · 1.1 KB
/
scripts.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
const cards = document.querySelectorAll('.memory-card');
let flippedCard = false;
let firstCard, secondCard;
function flipCard() {
this.classList.add('flip');
if (!flippedCard) {
flippedCard = true;
firstCard = this;
return;
}
flippedCard = false;
secondCard = this;
checkForMatch();
}
//CONFIRM MATCH
function checkForMatch() {
if (firstCard.dataset.img === secondCard.dataset.img) {
disableClick();
setTimeout(() => {
alert("It's a Match!");
}, 850);
} else {
//cards dont match
returnFaceDown();
}
}
// IF MATCH, CAN'T CLICK
function disableClick() {
firstCard.removeEventListener('click', flipCard);
secondCard.removeEventListener('click', flipCard);
}
// IF NO MATCH, FLIP FACE DOWN
function returnFaceDown() {
setTimeout(() => {
firstCard.classList.remove('flip');
secondCard.classList.remove('flip');
}, 1100);
}
// SHUFFLE CARDS --IIFE
(function () {
cards.forEach((card) => {
let cardNum = Math.floor(Math.random() * 8);
card.style.order = cardNum;
});
})();
cards.forEach((card) => card.addEventListener('click', flipCard));