-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
43 lines (39 loc) · 1.49 KB
/
script.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
document.addEventListener('DOMContentLoaded', () => {
const gameBoard = document.getElementById('game-board');
let cardValues = [];
let cardElements = [];
let matchesFound = 0;
function checkForMatch() {
if (cardElements[0].textContent === cardElements[1].textContent) {
cardElements.forEach(card => card.classList.add('matched'));
matchesFound += 2;
if (matchesFound === cardValues.length) {
alert('You won!');
}
} else {
cardElements.forEach(card => card.textContent = '');
}
cardElements = [];
}
function onCardClicked(e) {
if (cardElements.length < 2 && e.target.textContent === '') {
e.target.textContent = cardValues[e.target.dataset.index];
cardElements.push(e.target);
if (cardElements.length === 2) {
setTimeout(checkForMatch, 500);
}
}
}
fetch('http://localhost:5000/shuffle')
.then(response => response.json())
.then(cards => {
cardValues = cards;
cards.forEach((_, index) => {
const cardElement = document.createElement('div');
cardElement.classList.add('card');
cardElement.dataset.index = index;
cardElement.addEventListener('click', onCardClicked);
gameBoard.appendChild(cardElement);
});
});
});