-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
171 lines (152 loc) · 4.42 KB
/
script.js
File metadata and controls
171 lines (152 loc) · 4.42 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Seclet Boxes Container
let container = document.querySelector(".container"),
// Select All Boxes
boxes = Array.from(document.querySelectorAll(".container .box")),
// Create Array From All backs
allBack = Array.from(document.querySelectorAll(".container .back")),
countCorrect = 0,
countWrongs = 0,
// Audios
success = new Audio("success.mp4"),
fail = new Audio("fail.mp4"),
// Storing Clicked Boxes in Array
boxesSrcs = [],
// Storing Clicked Images in Array
imgsSrcs = [],
// Select Play Again Button
playAgain = document.querySelector(".play-again"),
correct = document.querySelector(".correct-count");
random();
boxes.forEach((box) => {
box.addEventListener("click", (e) => {
box.classList.add("flip");
// Add The Clicked Box to the Array
boxesSrcs.push(box);
// Get The Image from Clicked Box & Add it to the Array
imgsSrcs.push(box.querySelector('.back img').src)
// if a box clicked stop clicking on it
if(boxesSrcs.length == 1) {
boxesSrcs[0].classList.add("pointerfreeze")
}
// If There's Two Selected Boxes
else if (boxesSrcs.length == 2 ) {
container.classList.add("pointerfreeze")
// Stop Clicking Function
removeFreeze(container)
// If The Two Images are The Same
if (imgsSrcs[0] === imgsSrcs[1]) {
// If The Clicked Boxes Contain flip Class then Stop Clicking on it
boxesSrcs.forEach(box => box.classList.contains("flip") ? box.classList.add("pointerfreeze") : false);
countCorrect++;
countCorrectFn();
imgsSrcs.length = 0;
ifAllCorrect();
} else {
boxesSrcs[0].classList.remove("pointerfreeze")
countWrongs++;
countWrongsFn();
}
}
boxesSrcs.length == 2 ? boxesSrcs.length = 0 : false;
})
})
// Stop Clicking Function
function removeFreeze(container) {
setTimeout(() => {
container.classList.remove("pointerfreeze")
}, 1500);
}
// Remove flip Class
function removeFlip(box) {
setTimeout(() => {
box.classList.remove("flip")
}, 1300);
}
// Count Correct Attempts
function countCorrectFn() {
setTimeout(() => {
correct.innerHTML = `${countCorrect} / ${boxes.length / 2}`
success.play();
}, 1000);
}
// Count Wrong Tries
function countWrongsFn() {
setTimeout(() => {
wrongTries.innerText = countWrongs;
}, 1200);
boxesSrcs.forEach((box) => {
setTimeout(() => {
fail.play();
}, 1200);
removeFlip(box);
imgsSrcs.length = 0;
})
}
// Ask to Play Again
function playAgainFn(play) {
setTimeout(() => {
play.classList.add("show")
}, 4500);
boxes.forEach((box) => {
box.classList.remove("pointerfreeze")
})
}
// If Play Again Clicked
playAgain.addEventListener("click", (e) => {
playAgain.classList.remove("show")
boxes.forEach((box) => {
box.classList.remove("flip")
})
countCorrect = 0;
countWrongs = 0;
correct.innerHTML = `${countCorrect} / ${boxes.length / 2}`;
wrongTries.innerText = countWrongs;
random();
})
// Generate Random Confetti
let randomConfetti = () => {
let cont = document.createElement("div");
congrats.append(cont);
let createCongrats = setInterval(() => {
let confetti = document.createElement("div");
confetti.classList.add("confetti");
confetti.innerHTML = `<img src="imgs/confetti.png">`;
confetti.style.left = `${Math.random() * 100}%`;
confetti.style.animationDuration = `${(Math.random() + 0.5) * 1.5}s`;
cont.append(confetti);
}, 50);
setTimeout(() => {
clearInterval(createCongrats);
}, 3000);
setTimeout(() => {
cont.remove();
}, 5000);
}
// Chack If All Correct
function ifAllCorrect() {
if (countCorrect == boxes.length / 2) {
setTimeout(() => {
randomConfetti()
}, 1500);
playAgainFn(playAgain)
}
}
// Generate Random Images
function random() {
let orderedArr = [];
for (let i = 1; i <= 10; i++) {
orderedArr.push(`<img src="imgs/${i}.png" alt="candy-img">`, `<img src="imgs/${i}.png" alt="candy-img">`)
}
function rand(max) {
return Math.floor(Math.random() * max);
}
let ranomizedArr = [];
for (let i = 0; i < 20; i++) {
let random = rand(orderedArr.length);
ranomizedArr[i] = orderedArr[random];
orderedArr.splice(random, 1);
}
for (let i = 0; i < allBack.length; i++) {
allBack[i].innerHTML = ranomizedArr[i];
}
}