-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
84 lines (73 loc) · 2.34 KB
/
script.js
File metadata and controls
84 lines (73 loc) · 2.34 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
const $container = document.getElementById('container');
const $timer = document.getElementById('timer');
const $counterNumber = document.getElementById('counter-number');
const $clicker = document.getElementById('clicker');
const $message = document.getElementById('message');
const $timerScore = document.getElementById('timer-score');
const $reloadButton = document.getElementById('reload');
const $startButton = document.getElementById('start');
const maxTime = 5;
const winScore = 10;
let clicks;
let timerNumber;
let timeInterval;
const init = (e) => {
const fromButtonId = e.target.id;
clicks = 0;
timerNumber = maxTime;
$timer.innerHTML = timerNumber;
$counterNumber.innerHTML = clicks;
$message.classList.remove('message--failed');
$message.classList.remove('message--success');
$startButton.remove();
if (fromButtonId === 'start') {
timeInterval = setInterval(countDownTime, 1000);
$container.classList.add('started');
}
}
const reload = (e) => {
const fromButtonId = e.target.id;
clicks = 0;
timerNumber = maxTime;
$timer.innerHTML = timerNumber;
$counterNumber.innerHTML = clicks;
$timerScore.innerHTML = ``;
$message.classList.remove('message--failed');
$message.classList.remove('message--success');
timeInterval = setInterval(countDownTime, 1000);
$container.classList.add('started');
}
const countDownTime = () => {
timerNumber--;
$timer.innerHTML = timerNumber;
checkGameState();
}
const countUpClicks = () => {
if ($container.classList.contains('started')) {
clicks++;
$counterNumber.innerHTML = clicks;
checkGameState();
} else {
alert('You must click on the button first to start game!')
}
}
const checkGameState = () => {
if (timerNumber === 0) {
stopGame();
if (clicks < winScore) {
$message.classList.add('message--failed');
}
}
if (clicks >= winScore) {
stopGame();
$message.classList.add('message--success');
$timerScore.innerHTML = `Your time: ${(maxTime - timerNumber)} seconds.`;
}
}
const stopGame = () => {
clearInterval(timeInterval);
$container.classList.remove('started');
}
$clicker.addEventListener("click", countUpClicks);
$reloadButton.addEventListener("click", reload);
$startButton.addEventListener("click", init);