-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
127 lines (107 loc) · 3 KB
/
main.js
File metadata and controls
127 lines (107 loc) · 3 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const cards = document.querySelectorAll(".memory-card");
const playerLivesCount = document.querySelector("span");
let playerLives = 16;
// link text
playerLivesCount.textContent = playerLives;
let intialFlip = false;
let hasFlippedCard = false;
let lockBoard = false;
let firstCard, secondCard;
function flipCard() {
if (!intialFlip) {
intialFlip = true;
}
// locks board so you can't click more than 2 cards at a time
if (lockBoard) return;
// prevents double clicking on the same card
if (this === firstCard) return;
this.classList.add("flip");
// first click
if (!hasFlippedCard) {
// first click
hasFlippedCard = true;
firstCard = this;
return;
}
// second click
secondCard = this;
checkForMatch();
}
let count = 0;
function checkForMatch() {
// ternary operator is if/else statement in one line
let isMatch = firstCard.dataset.framework === secondCard.dataset.framework;
// condition ? true : false
isMatch ? disableCards() : unflipCards();
function disableCards() {
firstCard.removeEventListener("click", flipCard);
secondCard.removeEventListener("click", flipCard);
count++;
console.log("Function has been called " + count + " times.");
if (count === 12) {
winGame();
}
resetBoard();
}
function unflipCards() {
// lock the board
lockBoard = true;
setTimeout(() => {
firstCard.classList.remove("flip");
secondCard.classList.remove("flip");
// unlocks board after 1.5 seconds
resetBoard();
}, 1500);
playerLives--;
playerLivesCount.textContent = playerLives;
if (playerLives === 0)
setTimeout(() => {
gameOver();
}, 500);
}
function resetBoard() {
[hasFlippedCard, lockBoard] = [false, false];
[firstCard, secondCard] = [null, null];
}
}
(function shuffle() {
cards.forEach((card) => {
let randomPos = Math.floor(Math.random() * 18);
card.style.order = randomPos;
});
})();
// show message function
function showMessage(text, buttonText) {
setTimeout(() => {
const messageDiv = document.createElement("div");
messageDiv.setAttribute("id", "message");
messageDiv.textContent = text;
// create play again button
const playAgainBtn = document.createElement("button");
playAgainBtn.textContent = buttonText;
playAgainBtn.addEventListener("click", () => {
newGame();
messageDiv.style.opacity = "0";
setTimeout(() => {
messageDiv.remove();
}, 300);
});
// add play again button to message div
messageDiv.appendChild(playAgainBtn);
document.body.appendChild(messageDiv);
// animate message div
messageDiv.style.opacity = "1";
}, 500);
}
function gameOver() {
lockBoard = true;
showMessage("Bummer! 😵💫 You lose!", "Try again");
}
function winGame() {
showMessage("Awesome! 🤘 You Rock!", "Play again");
console.log("Count has reached 12.");
}
function newGame() {
location.reload();
}
cards.forEach((card) => card.addEventListener("click", flipCard));