-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
126 lines (111 loc) · 3.35 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
const homeScore = document.getElementById("home-score")
const guestScore = document.getElementById("guest-score")
const homeDisplay = document.getElementById("home-header")
const guestDisplay = document.getElementById("guest-header")
const periodDisplay = document.getElementById("period")
const newGameBtn = document.getElementById("new-game")
const pauseBtn = document.getElementById("pause-game")
const homeOne = document.getElementById("home-one")
const homeTwo = document.getElementById("home-two")
const homeThree = document.getElementById("home-three")
const guestOne = document.getElementById("guest-one")
const guestTwo = document.getElementById("guest-two")
const guestThree = document.getElementById("guest-three")
let home = 0
let guest = 0
let period = 1
let isPaused = false
homeScore.textContent = home
guestScore.textContent = guest
periodDisplay.textContent = period
disableButtons()
function newGame() {
home = 0
guest = 0
period = 1
isPaused = false
homeScore.textContent = home
guestScore.textContent = guest
homeDisplay.style.color = "white"
guestDisplay.style.color = "white"
document.getElementById('time').textContent = ""
startClock()
enableButtons()
}
function homePoints(points) {
home = home + points
homeScore.textContent = home
highlightLeader()
}
function guestPoints(points) {
guest = guest + points
guestScore.textContent = guest
highlightLeader()
}
function highlightLeader() {
if (home > guest) {
homeDisplay.style.color = "wheat"
guestDisplay.style.color = "white"
} else if (guest > home) {
homeDisplay.style.color = "white"
guestDisplay.style.color = "wheat"
} else {
homeDisplay.style.color = "white"
guestDisplay.style.color = "white"
}
}
function startTimer(duration, display) {
let timer = duration, minutes, seconds
let timerID = setInterval(function () {
if (!isPaused) {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10)
minutes = minutes < 10 ? "0" + minutes : minutes
seconds = seconds < 10 ? "0" + seconds : seconds
display.textContent = minutes + ":" + seconds
if (--timer < 0) {
period++
periodDisplay.textContent = period
timer = duration
}
if (period == 5) {
period = 4
periodDisplay.textContent = period
disableButtons()
clearInterval(timerID)
}
}
}, 1000)
}
function startClock() {
let tenMinutes = 60 * 10
display = document.getElementById('time')
startTimer(tenMinutes, display)
}
function disableButtons() {
homeOne.disabled = true
homeTwo.disabled = true
homeThree.disabled = true
guestOne.disabled = true
guestTwo.disabled = true
guestThree.disabled = true
}
function enableButtons() {
homeOne.disabled = false
homeTwo.disabled = false
homeThree.disabled = false
guestOne.disabled = false
guestTwo.disabled = false
guestThree.disabled = false
}
function pauseGame() {
if (isPaused) {
isPaused = false
enableButtons()
pauseBtn.textContent = "Pause game"
} else {
isPaused = true
disableButtons()
pauseBtn.textContent = "Resume game"
}
}