-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
166 lines (147 loc) · 3.97 KB
/
app.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const section = document.querySelector("section");
const playerLives = document.querySelector(".livesPlayer");
const resetButton = document.querySelector(".reset");
let countLives = 5;
playerLives.textContent = countLives;
const getCards = () => [
{
image: "images/panda1-01.png",
back: "images/pandafront-01.png",
name: "panda1",
},
{
image: "images/panda2-01.png",
back: "images/pandafront-01.png",
name: "panda2",
},
{
image: "images/panda3-01.png",
back: "images/pandafront-01.png",
name: "panda3",
},
{
image: "images/panda4-01.png",
back: "images/pandafront-01.png",
name: "panda4",
},
{
image: "images/panda5-01.png",
back: "images/pandafront-01.png",
name: "panda5",
},
{
image: "images/panda6-01.png",
back: "images/pandafront-01.png",
name: "panda6",
},
{
image: "images/panda1-01.png",
back: "images/pandafront-01.png",
name: "panda1",
},
{
image: "images/panda2-01.png",
back: "images/pandafront-01.png",
name: "panda2",
},
{
image: "images/panda3-01.png",
back: "images/pandafront-01.png",
name: "panda3",
},
{
image: "images/panda4-01.png",
back: "images/pandafront-01.png",
name: "panda4",
},
{
image: "images/panda5-01.png",
back: "images/pandafront-01.png",
name: "panda5",
},
{
image: "images/panda6-01.png",
back: "images/pandafront-01.png",
name: "panda6",
},
];
const randomizeCards = () => {
const cardData = getCards();
//how to randominize array in JS:
cardData.sort(() => Math.random() - 0.5);
return cardData;
};
const createCard = () => {
const cardData = randomizeCards();
cardData.forEach((e) => {
//create the cards on the html page
const card = document.createElement("div");
const backCard = document.createElement("img");
const frontCard = document.createElement("img");
card.classList = "card";
backCard.classList = "back";
frontCard.classList = "front";
frontCard.src = e.back;
backCard.src = e.image;
card.setAttribute("name", e.name);
section.appendChild(card);
card.appendChild(frontCard);
card.appendChild(backCard);
card.addEventListener("click", (e) => {
card.classList.toggle("toggleCard");
checkCards(e);
});
});
};
const checkCards = (e) => {
const clickedCard = e.target;
clickedCard.classList.add("flipped");
const flippedCards = document.querySelectorAll(".flipped");
const selectToggle = document.querySelectorAll(".toggleCard");
if (flippedCards.length === 2) {
if (
flippedCards[0].getAttribute("name") ===
flippedCards[1].getAttribute("name")
) {
flippedCards.forEach((card) => {
card.classList.remove("flipped");
card.style.pointerEvents = "none";
});
} else {
flippedCards.forEach((card) => {
card.classList.remove("flipped");
setTimeout(() => card.classList.remove("toggleCard"), 1000);
});
countLives--;
playerLives.textContent = countLives;
if (countLives === 0) {
restartGame("😭 Oh no, you lost the game 🐼");
}
}
}
if (selectToggle.length === 12) {
restartGame("🎉 Congratulations, you won 🎉🐼");
}
};
const restartGame = (text) => {
const cardData = randomizeCards();
const faces = document.querySelectorAll(".back");
const cards = document.querySelectorAll(".card");
section.style.pointerEvents = "none";
cardData.forEach((item, index) => {
cards[index].classList.remove("toggleCard");
setTimeout(() => {
cards[index].style.pointerEvents = "all";
faces[index].src = item.image;
cards[index].setAttribute("name", item.name);
section.style.pointerEvents = "all";
}, 1000);
});
countLives = 5;
playerLives.textContent = countLives;
setTimeout(() => window.alert(text), 100);
};
createCard();
const buttonAgain = resetButton.addEventListener("click", () => {
restartGame("Starting again! good luck!");
});