-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
86 lines (71 loc) · 2.39 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
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
// get all the checkboxes
const checkboxes = document.querySelectorAll('input[type="checkbox"]');
// add event listener to each checkbox
checkboxes.forEach(function(checkbox) {
checkbox.addEventListener('change', function() {
// get the number of checked checkboxes
const checkedCount = document.querySelectorAll('input[type="checkbox"]:checked').length;
// update the counter display
const counter = document.querySelector('#counter');
counter.textContent = checkedCount;
});
});
// Timer
// get all the checkboxes
// add event listener to each checkbox
checkboxes.forEach(function(checkbox) {
checkbox.addEventListener('change', function() {
// get the number of checked checkboxes
const checkedCount = document.querySelectorAll('input[type="checkbox"]:checked').length;
// update the counter display
const counter = document.querySelector('#counter');
counter.textContent = checkedCount;
});
});
// Timer
const counter = document.getElementById('counter');
const timer = document.getElementById('timer');
let startTime;
let timerInterval;
function startTimer() {
startTime = new Date().getTime();
timerInterval = setInterval(updateTimer, 1000);
}
function updateTimer() {
const elapsedTime = new Date().getTime() - startTime;
const seconds = Math.floor(elapsedTime / 1000);
const minutes = Math.floor(seconds / 60);
timer.textContent = `${minutes.toString().padStart(2, '0')}:${(seconds % 60).toString().padStart(2, '0')}`;
}
let numChecked = 0;
checkboxes.forEach(function(checkbox) {
checkbox.addEventListener('click', function() {
if (numChecked === 0) {
startTimer();
}
if (checkbox.checked) {
numChecked++;
} else {
numChecked--;
}
counter.textContent = numChecked;
if (numChecked === checkboxes.length) {
clearInterval(timerInterval);
alert(`You cracked all the eggs in ${timer.textContent}!`);
}
});
});
// Hide and show articles
const startButton = document.querySelector('#startScreen button');
const gameScreen = document.querySelector('#gameScreen');
const returnButton = document.querySelector('#gameScreen button');
startButton.addEventListener('click', () => {
// hide the start screen
startScreen.style.display = 'none';
// show the game screen
gameScreen.style.display = 'block';
});
returnButton.addEventListener('click', () => {
// reset the game
location.reload();
});