-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (76 loc) · 2.32 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
let homeScore = 0;
let guestScore = 0;
let homeFouls = 0;
let guestFouls = 0;
let period = 1;
let timer = 12 * 60; // 12 minutes in seconds
let timerInterval;
function updateScores(){
document.getElementById("home-score").textContent = homeScore;
document.getElementById("guest-score").textContent = guestScore;
if (homeScore > questScore) {
document.getElementById("home").classList.add("leader");
document.getElementById("guest").classList.remove("leader");
} else if (guestScore > homeScore) {
document.getElementById("guest").classList.add("leader");
document.getElementById("home").classList.remove("leader");
} else {
document.getElementById("home").classList.remove("leader");
document.getElementById("guest").classList.remove("leader");
}
}
function incrementScore(team) {
if (team === 'home') {
homeScore++;
} else {
guestScore++
}
updateScores();
}
function addHomeFoul() {
document.getElementById("home-fouls").textContent = homeFouls;
homeFouls++;
}
function addGuestFoul() {
document.getElementById("guest-fouls").textContent = guestFouls;
guestFouls++;
}
function newGame() {
homeScore = 0;
guestScore = 0;
homeFouls = 0;
guestFouls = 0;
period = 0;
timer = 12 * 60;
clearInterval(timerInterval);
updateScores();
document.getElementById("home-fouls").textContent = homeFouls;
document.getElementById("guest-fouls").textContent = guestFouls;
document.getElementById("period").textContent = period;
document.getElementById("timer").textContent = formatTime(timer);
}
function nextPeriod() {
if (period < 4) {
period++;
document.getElementById("period").textContent = period;
} else {
alert("Game Over! No more periods.")
}
}
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const secs = seconds % 60;
return `${minutes}:${secs < 10 ? '0' : ''}${secs}`;
}
function startTimer() {
if (timerInterval) clearInterval(timerInterval);
timerInterval = setInterval(() => {
if (timer > 0) {
timer--;
document.getElementById("timer").textContent = formatTime(timer);
} else {
clearInterval(timerInterval);
alert("End of period!");
}
}, 1000);
}