-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
53 lines (47 loc) · 1.82 KB
/
script.js
File metadata and controls
53 lines (47 loc) · 1.82 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
document.addEventListener('DOMContentLoaded', function() {
// collapsible functionality
const colls = document.querySelectorAll('.collapsible');
colls.forEach(coll => {
coll.addEventListener('click', function() {
this.classList.toggle('active');
const content = this.nextElementSibling;
if (content.style.display === 'block') {
content.style.display = 'none';
} else {
content.style.display = 'block';
}
});
});
// function to calculate total
function calculateTotal() {
const values = [0.025, 0.0357, 0.025, 0.03035, 0.0357, 0.05, 0.05, 0.0556, 0.0625, 0.075, 0.08, 0.1, 0.125, 0.178, 0.249, 0.251, 0.3, 0.3751, 0.375, 0.428, 0.5, 1]; // All spike variables' respective values
let sum = 0;
for (let i = 1; i <= values.length; i++) {
let inputElement = document.getElementById('count' + i);
if (!inputElement) {
console.error('No input found with id:', 'count' + i);
continue; // Skip this iteration if the input is missing
}
let count = parseInt(inputElement.value) || 0;
sum += values[i - 1] * count;
}
document.getElementById('total').innerText = sum.toFixed(2);
}
// number changed?
document.querySelectorAll('.calculator input[type="number"]').forEach(input => {
input.addEventListener('input', calculateTotal);
});
// button clicked?
document.querySelectorAll('.controls button').forEach(button => {
button.addEventListener('click', function() {
const input = this.parentElement.querySelector('input[type="number"]');
let value = parseInt(input.value) || 0;
if (this.classList.contains('increment')) {
input.value = value + 1;
} else if (this.classList.contains('decrement') && value > 0) {
input.value = value - 1;
}
calculateTotal();
});
});
});