-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
59 lines (49 loc) · 2.08 KB
/
script.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
const start = document.getElementById('startButton');
start.addEventListener('click', startGame);
function startGame() {
document.getElementById('startButton').style.display = 'none';
const boxes = document.querySelectorAll('.box');
const numbers = shuffle(Array.from({ length: 9 }, (_, i) => i + 1));
const message = document.querySelector('.message');
boxes.forEach((box, i) => {
box.querySelector('.num').textContent = numbers[i];
box.classList.add('flipped');
});
message.textContent = 'Memorize the numbers!';
setTimeout(() => {
boxes.forEach(box => box.classList.remove('flipped'));
message.textContent = 'Now, click on the boxes in sequence!';
let currentIndex = 1;
boxes.forEach(box => {
box.addEventListener('click', function checkClick() {
const num = parseInt(box.querySelector('.num').textContent);
if (num === currentIndex) {
box.classList.add('flipped');
currentIndex++;
if (currentIndex > 9) {
message.textContent = 'Congratulations! You won!';
start.style.display = 'block';
start.textContent = 'New Game';
start.addEventListener('click',(e)=>{
boxes.forEach(b => b.classList.remove('flipped'));
currentIndex = 1;
})
}
} else {
message.textContent = 'Wrong sequence! Try again!';
boxes.forEach(b => b.classList.remove('flipped'));
currentIndex = 1;
start.textContent = "Try Again";
start.style.display = 'block';
}
});
});
}, 3000);
}
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}