Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
130 lines (114 loc) · 3.1 KB
/
Copy pathscript.js
File metadata and controls
130 lines (114 loc) · 3.1 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
128
129
130
import cards from "./cards.js";
const gameWrap = document.querySelector(".game-wrap");
const grid = document.createElement("div");
const resetButton = document.querySelector(".reset");
const stepNumber = document.querySelector(".step-number");
const stepWrap = document.querySelector(".step-wrap");
let count = 0;
let step = 0;
let firstChoice = "";
let secondChoice = "";
let matched = [];
const duplicatedCards = cards.concat(cards);
const shuffledCards = duplicatedCards.sort(() => {
return 0.5 - Math.random();
});
grid.setAttribute("class", "grid");
gameWrap.appendChild(grid);
shuffledCards.map((item) => {
const card = document.createElement("div");
card.classList.add("card");
card.dataset.name = item.name;
card.style.backgroundImage = `url(${item.img})`;
const front = document.createElement("div");
front.classList.add("face");
grid.appendChild(card);
card.appendChild(front);
});
const handleClick = (e) => {
const clickedCard = e.target.closest(".card");
const same = clickedCard?.classList.contains("selected");
if (clickedCard && count < 2 && !same) {
count++;
checkTwoChoices(clickedCard);
}
};
const reveal = (clickedCard) => {
clickedCard.classList.add("selected");
};
const checkTwoChoices = (clickedCard) => {
// break if you click on already matched card
if (clickedCard.classList.contains("matched")) {
count = 0;
return;
}
// set First
if (count === 1) {
firstChoice = clickedCard.dataset.name;
reveal(clickedCard);
}
// setSecond
if (count === 2 && !clickedCard.classList.contains("selected")) {
secondChoice = clickedCard.dataset.name;
reveal(clickedCard);
setTimeout(() => {
resetCards();
}, 700);
step++;
grid.dispatchEvent(new CustomEvent("stepUpdated"));
}
// compare
if (firstChoice === secondChoice) {
matchCards();
setTimeout(() => {
resetCards();
}, 700);
}
};
const renderStep = () => {
stepNumber.innerText = step;
};
const matchCards = () => {
const selected = document.querySelectorAll(".selected");
selected.forEach((card) => {
card.classList.add("matched");
matched.push(card);
grid.dispatchEvent(new CustomEvent("updateMatched"));
});
};
const resetCards = () => {
count = 0;
firstChoice = "";
secondChoice = "";
const selected = document.querySelectorAll(".selected");
selected.forEach((card) => {
card.classList.remove("selected");
});
};
const resetGame = () => {
matched.forEach((card) => {
card.classList.remove("matched");
});
step = 0;
stepWrap.classList.remove("success");
grid.dispatchEvent(new CustomEvent("stepUpdated"));
const successText = document.querySelector(".success-text");
stepWrap.removeChild(successText);
};
const success = () => {
stepWrap.classList.add("success");
const success = document.createElement("span");
success.setAttribute("class", "success-text");
success.innerText = "Congrats!";
stepWrap.prepend(success);
};
grid.addEventListener("click", handleClick);
grid.addEventListener("stepUpdated", renderStep);
grid.addEventListener("updateMatched", () => {
if (matched.length === shuffledCards.length) {
success();
}
});
resetButton.addEventListener("click", () => {
resetGame();
});