This repository was archived by the owner on Nov 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
93 lines (84 loc) · 3.28 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
87
88
89
90
91
92
93
// get the table element and number of stations input
const scorecard = document.getElementById('scorecard');
const numStationsInput = document.getElementById('num-stations');
// add event listener for number of stations input changes
numStationsInput.addEventListener('input', updateStations);
// function to update the number of stations
function updateStations() {
const numStations = numStationsInput.valueAsNumber;
const headerRow = scorecard.rows[0];
// update header row with new number of stations
for (let i = headerRow.cells.length - 2; i > 0; i--) {
headerRow.deleteCell(i);
}
for (let i = 1; i <= numStations; i++) {
const stationCell = document.createElement('th');
stationCell.textContent = `Station ${i}`;
headerRow.appendChild(stationCell);
}
// update shooter rows with new number of stations
for (let i = 1; i < scorecard.rows.length; i++) {
const shooterRow = scorecard.rows[i];
for (let j = shooterRow.cells.length - 2; j > 0; j--) {
shooterRow.deleteCell(j);
}
for (let j = 1; j <= numStations; j++) {
const stationCell = document.createElement('td');
stationCell.setAttribute('contenteditable', true);
shooterRow.appendChild(stationCell);
}
}
calculateTotals();
}
// get the number of shooters input and add shooter button
const numShootersInput = document.getElementById('num-shooters');
const addShooterButton = document.getElementById('add-shooter');
// add event listener for number of shooters input changes
numShootersInput.addEventListener('input', updateShooters);
// add event listener for add shooter button click
addShooterButton.addEventListener('click', addShooter);
// function to update the number of shooters
function updateShooters() {
const numShooters = numShootersInput.valueAsNumber;
const shooterNames = document.getElementById('shooter-names');
const shooterRows = scorecard.tBodies[0].rows;
// update shooter rows with new number of shooters
while (shooterRows.length > numShooters) {
scorecard.tBodies[0].deleteRow(shooterRows.length - 1);
}
while (shooterRows.length < numShooters) {
const newRow = scorecard.tBodies[0].insertRow();
const nameCell = newRow.insertCell();
nameCell.textContent = `Shooter ${shooterRows.length}`;
for (let i = 1; i <= numStationsInput.valueAsNumber; i++) {
const stationCell = newRow.insertCell();
stationCell.setAttribute('contenteditable', true);
}
const totalCell = newRow.insertCell();
totalCell.classList.add('total-cell');
}
calculateTotals();
}
// function to add a shooter
function addShooter() {
numShootersInput.valueAsNumber++;
updateShooters();
}
// add event listeners for score cells
scorecard.addEventListener('input', calculateTotals);
// function to calculate the totals for each shooter
function calculateTotals() {
const shooterRows = scorecard.tBodies[0].rows;
for (let i = 0; i < shooterRows.length; i++) {
const shooterRow = shooterRows[i];
let total = 0;
for (let j = 1; j < shooterRow.cells.length - 1; j++) {
const cellValue = shooterRow.cells[j].textContent.trim();
const score = cellValue === 'X' ? 1 : parseInt(cellValue);
if (!isNaN(score)) {
total += score;
}
}
shooterRow.cells[shooterRow.cells.length - 1].textContent = total;
}
}