Skip to content

Commit 37fe7fa

Browse files
committed
only click two cards at a time
1 parent 30448f7 commit 37fe7fa

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

script/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
11
const clearAllTimeouts = (setTimeoutsId) => {
22
for (const timeoutId of setTimeoutsId) {
3-
clearInterval(timeoutId);
3+
clearTimeout(timeoutId);
44
}
55

66
setTimeoutsId.clear();
77
};
88

99
const closeAllOpenedCards = (clickedCards) => {
1010
return setTimeout(() => {
11-
if (areColorMatches(clickedCards)) removeCards(clickedCards);
11+
if (areColorMatches(clickedCards)) {
12+
removeCards(clickedCards);
13+
return;
14+
};
15+
1216
for (const clickedCard of clickedCards) {
13-
clickedCard.classList.remove('open_card');
17+
clickedCard.classList.add('closed_cards');
1418
}
1519
clickedCards.clear();
1620
}, 2000);
1721
};
1822

1923
const openCards = (clickedCards) => {
2024
for (const clickedCard of clickedCards) {
21-
clickedCard.classList.add('open_card');
25+
clickedCard.classList.remove('closed_cards');
2226
}
2327
};
2428

2529
const areColorMatches = ([...clickedCards]) => {
2630
return clickedCards[0].className === clickedCards[1]?.className;
27-
2831
};
2932

3033
const removeCards = (clickedCards) => {
3134
for (const clickedCard of clickedCards) {
32-
clickedCard.style.display = 'none';
35+
clickedCard.classList.add('remove_card');
3336
}
37+
clickedCards.clear();
3438
};
3539

3640
const startGame = () => {
3741
const setTimeoutsId = new Set();
3842
const clickedCards = new Set();
3943

4044
addEventListener("click", (event) => {
41-
if (!(event.target.classList.contains('card'))) return;
45+
if (!(event.target.classList.contains('card')) || clickedCards.size >= 2) return;
4246

4347
clickedCards.add(event.target);
4448
openCards(clickedCards);

0 commit comments

Comments
 (0)