-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
164 lines (154 loc) · 4.13 KB
/
index.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
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
/* eslint-disable no-restricted-syntax */
/* eslint-disable linebreak-style */
// !TODO : score board , theme of cards
const cardContainer = document.querySelector('.card-container');
const winz = document.querySelector('.winz');
const losez = document.querySelector('.losez');
const reload = document.querySelector('.reload');
const scores = document.querySelector('.scores');
const inputy = document.querySelector('input');
reload.addEventListener('click', () => {
location.reload();
});
const [smallBtn, mediumBtn, largeBtn] = document.querySelectorAll('button');
let wins = 0;
let loseesss = 0;
let initalCardsNumbers = 0;
let userName = '';
const btnList = [smallBtn, mediumBtn, largeBtn];
smallBtn.addEventListener('click', () => {
initalCardsNumbers = 12;
safe(12);
startTimer();
userName = inputy.value;
console.log(userName);
reload.style.visibility = 'visible';
for (const b of btnList) {
b.classList.add('disapear');
}
});
mediumBtn.addEventListener('click', () => {
initalCardsNumbers = 24;
safe(24);
startTimer();
userName = inputy.value;
reload.style.visibility = 'visible';
for (const b of btnList) {
b.classList.add('disapear');
}
});
largeBtn.addEventListener('click', () => {
initalCardsNumbers = 36;
safe(36);
startTimer();
userName = inputy.value;
reload.style.visibility = 'visible';
for (const b of btnList) {
b.classList.add('disapear');
}
});
console.log('🏊 ');
const cardsArray = [];
let currentData = 0;
let currentCards = [];
const createCards = (numOfCards) => {
for (let i = 0; i < numOfCards; i++) {
const card = document.createElement('div');
card.classList.add('card');
if (i % 2 === 0) {
currentData = i;
}
card.setAttribute('data', `${currentData}`);
// card.innerHTML = `${currentData}`;
// ev on cards
card.addEventListener('click', (e) => {
const cardy = e.currentTarget;
console.log(currentCards);
if (currentCards.length < 2 && !currentCards.includes(cardy)) {
currentCards.push(cardy);
cardy.classList.remove('hide-card');
tick();
// console.log(cardy.getAttribute('data'));
}
});
cardsArray.push(card);
}
};
function clearCurrentList() {
setTimeout(() => {
currentCards = [];
}, 1000);
}
function checkForWin() {
if (wins === initalCardsNumbers / 2) {
alert('you won!');
localStorage.setItem(userName, `${wins - loseesss}`);
console.log(Object.entries(localStorage));
location.reload();
}
}
async function waitFortime() {
await hideCards2();
await clearCurrentList();
}
function hideCards2() {
for (const card of currentCards) {
setTimeout(() => {
card.classList.add('hide-card');
}, 1000);
}
}
function shuffle(array) {
let currentIndex = array.length;
let temporaryValue;
let randomIndex;
// While there remain elements to shuffle...
while (currentIndex !== 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function appendCards() {
for (const card of cardsArray) {
cardContainer.append(card);
}
}
async function hideCards() {
for (const card of cardsArray) {
await card.classList.add('hide-card');
}
}
// *COMP
function tick() {
if (currentCards[1]) {
if (
currentCards[0].getAttribute('data')
=== currentCards[1].getAttribute('data')
) {
for (const card of currentCards) {
card.classList.add('disapear');
}
waitFortime();
wins++;
winz.innerHTML = wins;
checkForWin();
} else {
waitFortime();
loseesss++;
losez.innerHTML = loseesss;
}
}
}
async function safe(numberOfCards) {
await createCards(numberOfCards);
await shuffle(cardsArray);
await hideCards();
await appendCards();
}
// safe(12); // call