-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
107 lines (76 loc) · 2.15 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
// shuffle
function shuffle(list) {
for (let i = 0; i < list.length; i++) {
let rnd = Math.floor(Math.random() * list.length)
let tmp = list[i];
list[i] = list[rnd];
list[rnd] = tmp;
}
return list
}
// dom selection
let li = document.querySelectorAll("li");
let counterSpan = document.getElementById("userMoveCounter");
let ul = document.querySelector("ul");
let counter = 0;
let openCards = [];
let matchedCards = [];
startGame();
// functions
function showIcon() {
// dom walking
// this.firstChild.classList.add("show")
openCards.push(this);
this.classList.add("show");
if (openCards.length === 2)
compareCards()
// user Move
counter++;
counterSpan.textContent = counter;
}
function Matched() {
openCards[0].classList.add("matched");
openCards[1].classList.add("matched");
// matchedCards = [...openCards];
matchedCards.push(openCards[0])
matchedCards.push(openCards[1])
if (matchedCards.length === 16)
winGame()
}
function Unmatched() {
openCards[0].classList.add("unmatched");
openCards[1].classList.add("unmatched");
setTimeout(function () {
openCards[0].classList.remove("unmatched");
openCards[1].classList.remove("unmatched");
openCards[0].classList.remove("show");
openCards[1].classList.remove("show");
openCards = []
}, 1000)
}
function compareCards() {
if (openCards[0].innerHTML === openCards[1].innerHTML) {
Matched()
openCards = [];
} else {
Unmatched();
}
}
function winGame() {
alert(`YOU WIN by ${counter} move`)
}
function startGame() {
// clone into a vriable
let lis = [...li];
console.log(li);
console.log(shuffle(lis));
ul.innerHTML = "";
for (const li of lis) {
console.log(li.outerHTML)
ul.innerHTML += li.outerHTML
}
lis = document.querySelectorAll("li");
for (const li of lis) {
li.addEventListener("click", showIcon);
}
}