-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
107 lines (94 loc) · 2.67 KB
/
script.js
File metadata and controls
107 lines (94 loc) · 2.67 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
const cards = document.querySelectorAll('.memory-card');
const timer = document.querySelector('.timer');
const movesCounter = document.querySelector('.moves');
let hasFlippedCard = false;
let firstCard, secondCard;
let lockBoard = false;
let seconds = 0,
minutes = 0,
hours = 0;
let moves = 0;
let interval;
// Immediately invoked function expression(IIFE)
// @description Shuffle based on order of item in the flex box: this is an easier way to shuffle the cards
(function shuffle() {
cards.forEach(card => {
let randomOrder = Math.floor(Math.random() * 12);
card.style.order = randomOrder;
});
})();
(function startGame() {
[moves, seconds, minutes, hours] = [0, 0, 0, 0];
timer.innerHTML = "0:0";
clearInterval(interval);
})();
function restartGame() {
cards.forEach(card => {
card.classList.remove('flip');
card.addEventListener('click', flipCard);
let randomOrder = Math.floor(Math.random() * 12);
card.style.order = randomOrder;
});
[moves, seconds, minutes, hours, firstCard, secondCard, hasFlippedCard, lockBoard] = [0, 0, 0, 0, null, null, false, false];
timer.innerHTML = "0:0";
clearInterval(interval);
}
function flipCard() {
if (lockBoard) return;
if (this === firstCard) return;
this.classList.add('flip');
if (!hasFlippedCard) {
hasFlippedCard = true;
firstCard = this;
return;
}
secondCard = this;
checkForMatch();
}
function checkForMatch() {
let isMatch = firstCard.dataset.name === secondCard.dataset.name;
isMatch ? disableCards() : unFlipCards();
addMove();
}
function disableCards() {
firstCard.removeEventListener('click', flipCard);
secondCard.removeEventListener('click', flipCard);
resetValuesAttachedToCard();
}
function unFlipCards() {
lockBoard = true
setTimeout(() => {
firstCard.classList.remove('flip');
secondCard.classList.remove('flip');
resetValuesAttachedToCard();
}, 700);
}
function resetValuesAttachedToCard() {
[hasFlippedCard, lockBoard] = [false, false];
[firstCard, secondCard] = [null, null];
}
function addMove() {
moves++;
movesCounter.innerHTML = moves;
if (moves == 1) {
seconds = 0;
minutes = 0;
hours = 0;
startTimer();
}
}
function startTimer() {
interval = setInterval(() => {
seconds++;
timer.innerHTML = minutes + ":" + seconds;
if (seconds == 60) {
minutes++;
seconds = 0;
}
if (minutes = 60) {
hours++;
minutes = 0;
}
}, 1000);
}
cards.forEach(card => card.addEventListener('click', flipCard));