-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
191 lines (159 loc) · 5.51 KB
/
main.js
File metadata and controls
191 lines (159 loc) · 5.51 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const CONTAINER = document.getElementById('container');
let range = document.getElementById('range');
let option = document.getElementById('option');
const totalContainer = document.getElementById('total');
const totalItemsContainer = document.getElementById('total-items');
const timer = document.getElementById('timer')
const timerCounter = document.getElementById('timer-counter')
const play = document.getElementById('play');
let time = 1000;
let counter = 1;
range.addEventListener('change', () => {
let rangeValue = range.value;
totalContainer.innerHTML = rangeValue;
if (rangeValue == 9) {
totalItemsContainer.innerHTML = "5";
} else if (rangeValue == 12) {
totalItemsContainer.innerHTML = "5";
} else if (rangeValue == 15) {
totalItemsContainer.innerHTML = "8";
}
});
function setTime(myEl, difficulty) {
let diffuculties = document.querySelectorAll('.difficulty');
diffuculties.forEach((el) => {
el.classList.remove('difficultyActive')
});
if (difficulty == "1") {
time = 1000;
myEl.classList.add('difficultyActive')
} else if (difficulty == "2") {
time = 0500;
myEl.classList.add('difficultyActive')
} else {
time = 0300;
myEl.classList.add('difficultyActive')
} this
}
play.addEventListener('click', () => {
let number = range.value;
let generate = 0;
let rand = 9;
if (number == 9) {
CONTAINER.style.gridTemplateColumns = "1fr 1fr 1fr";
CONTAINER.style.gridTemplateRows = "1fr 1fr 1fr";
generate = 5;
} else if (number == 12) {
CONTAINER.style.gridTemplateColumns = "1fr 1fr 1fr 1fr";
CONTAINER.style.gridTemplateRows = "1fr 1fr 1fr";
rand = 12;
generate = 5;
} else if (number == 15) {
CONTAINER.style.gridTemplateColumns = "1fr 1fr 1fr 1fr 1fr";
CONTAINER.style.gridTemplateRows = "1fr 1fr 1fr";
rand = 15;
generate = 8;
}
// Generate random numbers
numbers = []
positions = []
for (let i = 1; i <= generate; i++) {
numbers.push(i);
}
while (positions.length < generate) {
let random = Math.floor(Math.random() * rand) + 0
if (positions.indexOf(random) === -1) {
positions.push(random)
}
}
console.log(numbers)
console.log(positions)
for (let i = 0; i < number; i++) {
let box = document.createElement('div');
box.classList.add('box', 'add', 'boxHover');
box.setAttribute('onclick', `clickMe(this, '${generate}')`)
const label = document.createElement('h1');
label.classList.add("label");
box.style.pointerEvents = "none";
// SET THE POSITION OF GENERATED NUMBER
if (positions.indexOf(i) != -1) {
box.setAttribute('data-value', numbers[positions.indexOf(i)])
box.appendChild(label)
label.textContent = numbers[positions.indexOf(i)]
} else {
box.setAttribute('data-value', 0)
}
CONTAINER.appendChild(box);
}
option.style.display = "none";
// Countdown starting value
let countdown = 3;
// Update the timer h1 element with the current countdown value
timer.style.display = "flex"
timerCounter.innerText = countdown;
// Set interval to countdown every second
let countdownInterval = setInterval(() => {
countdown--;
timerCounter.innerText = countdown; // Update the timer on screen
if (countdown === 0) {
clearInterval(countdownInterval); // Stop the countdown when it reaches 0
timer.style.display = "none"; // Hide the timer
displayNumbers(number); // Call the function to display boxes
}
}, 1000);
});
function displayNumbers() {
let label = document.querySelectorAll('.label');
let allBoxes = document.querySelectorAll('.box');
label.forEach((myEl) => {
myEl.style.visibility = "visible";
})
setTimeout(() => {
label.forEach((myEl) => {
myEl.style.visibility = "hidden";
})
}, time);
allBoxes.forEach((myEl) => {
myEl.style.pointerEvents = "fill";
})
}
function clickMe(el, limit) {
let num = parseInt(limit);
let allBoxes = document.querySelectorAll('.box');
let allLabels = document.querySelectorAll('.label');
if (counter <= num) {
let value = parseInt(el.getAttribute("data-value"));
console.log("Clicked box value:", value);
if (value == counter) {
counter++;
let label = el.querySelector('.label');
if (label) {
label.style.visibility = "visible";
}
el.classList.add('green');
el.classList.remove('boxHover');
if (counter == num + 1) {
allBoxes.forEach((myEl) => {
myEl.classList.add('green');
myEl.classList.remove('boxHover');
})
setTimeout(() => {
location.reload();
}, 3000);
}
} else {
allBoxes.forEach((myEl) => {
myEl.classList.add('red');
myEl.classList.remove('green');
myEl.classList.remove('boxHover');
myEl.style.pointerEvents = "none";
})
allLabels.forEach((myEl) => {
myEl.style.visibility = "visible";
})
setTimeout(() => {
location.reload();
}, 3000);
}
}
}