Expense Tool
Expense Tool
<script>
// Define an array to hold expenses
let expenses = [];
// Function to add an expense to the array
function addExpense() {
const description = document.getElementById("description").value;
const amount = Number(document.getElementById("amount").value);
expenses.push({ description, amount });
updateExpenses();
}
// Function to update the total expenses
function updateExpenses() {
const totalExpenses = document.getElementById("totalExpenses");
let total = 0;
expenses.forEach(expense => {
total += expense.amount;
});
totalExpenses.textContent = `Total expenses: $${total}`;
}
</script>