-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscripttracker.js
More file actions
70 lines (59 loc) · 2.37 KB
/
Copy pathscripttracker.js
File metadata and controls
70 lines (59 loc) · 2.37 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
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('budget-form');
const budgetItems = document.getElementById('budget-items');
const progressBars = document.getElementById('progress-bars');
let budgets = [];
form.addEventListener('submit', (event) => {
event.preventDefault();
const category = document.getElementById('category').value;
const amount = parseFloat(document.getElementById('amount').value);
if (category && !isNaN(amount)) {
// Add new budget
budgets.push({ category, amount, spent: 0 });
updateBudgetList();
updateProgressBars();
// Clear form
form.reset();
}
});
function updateBudgetList() {
budgetItems.innerHTML = '';
budgets.forEach((budget, index) => {
const listItem = document.createElement('li');
listItem.innerHTML = `
<span>${budget.category}: $${budget.amount.toFixed(2)}</span>
<button onclick="addExpense(${index})">Add Expense</button>
`;
budgetItems.appendChild(listItem);
});
}
function updateProgressBars() {
progressBars.innerHTML = '';
budgets.forEach(budget => {
const progressBar = document.createElement('div');
progressBar.classList.add('progress-bar');
const percentage = (budget.spent / budget.amount) * 100;
progressBar.innerHTML = `
<span style="width: ${percentage.toFixed(2)}%">${percentage.toFixed(2)}%</span>
`;
progressBars.appendChild(progressBar);
});
}
window.addExpense = function (index) {
const amount = parseFloat(prompt('Enter expense amount:'));
if (!isNaN(amount) && amount > 0) {
if (budgets[index].spent + amount <= budgets[index].amount) {
budgets[index].spent += amount;
updateBudgetList();
updateProgressBars();
} else {
alert('Expense exceeds the budget amount!');
}
}
}
});
const goBackButton = document.getElementById('goBackButton');
goBackButton.addEventListener('click', () => {
// Go back in history
window.history.back();
});