-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
35 lines (27 loc) · 1 KB
/
main.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
const expenseInput = document.getElementById('expense');
const amountInput = document.getElementById('amount');
const addButton = document.getElementById('addButton');
const expenseList = document.getElementById('expenseList');
const totalExpenses = document.getElementById('totalExpenses');
let expenses = [];
let total = 0;
addButton.addEventListener('click', () => {
const expense = expenseInput.value.trim();
const amount = parseFloat(amountInput.value);
if (expense !== '' && !isNaN(amount)) {
expenses.push({ expense, amount });
total += amount;
updateUI();
}
expenseInput.value = '';
amountInput.value = '';
});
function updateUI() {
expenseList.innerHTML = '';
expenses.forEach(expenseObj => {
const listItem = document.createElement('li');
listItem.textContent = `${expenseObj.expense}: $${expenseObj.amount.toFixed(2)}`;
expenseList.appendChild(listItem);
});
totalExpenses.textContent = total.toFixed(2);
}