This repository was archived by the owner on Jan 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
63 lines (55 loc) · 2.14 KB
/
app.js
File metadata and controls
63 lines (55 loc) · 2.14 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
document.addEventListener("DOMContentLoaded", () => {
const grid = document.querySelector(".grid");
const resultDisplay = document.querySelector("#result");
let cardsChosen = [];
let cardsChosenId = [];
let wonCards = [];
const cardsArray = [
{ name: "cheeseburger", img: "images/cheeseburger.png" },
{ name: "fries", img: "images/fries.png" },
{ name: "hotdog", img: "images/hotdog.png" },
{ name: "ice-cream", img: "images/ice-cream.png" },
{ name: "milkshake", img: "images/milkshake.png" },
{ name: "pizza", img: "images/pizza.png" },
]
.reduce((acc, element) => [...acc, element, element], [])
.sort(() => 0.5 - Math.random());
const checkMatch = () => {
const allCards = document.querySelectorAll("img");
const firstCard = cardsChosen[0];
const secondCard = cardsChosen[1];
if (firstCard === secondCard) {
allCards[cardsChosenId[0]].classList.add("invisible");
allCards[cardsChosenId[1]].classList.add("invisible");
} else {
allCards[cardsChosenId[0]].setAttribute("src", `images/blank.png`);
allCards[cardsChosenId[1]].setAttribute("src", `images/blank.png`);
}
// check if its a match
if (firstCard === secondCard) {
wonCards = [...wonCards, firstCard];
resultDisplay.textContent = wonCards.length;
}
// check for finished match
if (wonCards.length === allCards.length / 2) alert("ya ganaste puñetas");
// reset arrays
cardsChosen = [];
cardsChosenId = [];
};
const flipCard = (img, name, { target }) => {
if (target.classList.contains("invisible")) return;
const cardId = target.getAttribute("data-id");
target.setAttribute("src", img);
cardsChosen = [...cardsChosen, name];
cardsChosenId = [...cardsChosenId, cardId];
if (cardsChosen.length === 2) setTimeout(checkMatch, 500);
};
// fill cards grid
cardsArray.forEach(({ img, name }, idx) => {
const card = document.createElement("img");
card.setAttribute("src", "images/blank.png");
card.setAttribute("data-id", idx);
card.addEventListener("click", (event) => flipCard(img, name, event));
grid.appendChild(card);
});
});