-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
40 lines (33 loc) · 1.34 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
let totalIncome = 0;
let totalExpenses = 0;
function addIncome() {
const incomeInput = document.getElementById("income");
const incomeAmount = parseFloat(incomeInput.value);
if (!isNaN(incomeAmount) && incomeAmount > 0) {
totalIncome += incomeAmount;
updateBudgetDisplay();
incomeInput.value = "";
} else {
alert("Please enter a valid positive number for income.");
}
}
function addExpense() {
const expenseInput = document.getElementById("expense");
const expenseAmount = parseFloat(expenseInput.value);
if (!isNaN(expenseAmount) && expenseAmount > 0) {
totalExpenses += expenseAmount;
updateBudgetDisplay();
expenseInput.value = "";
} else {
alert("Please enter a valid positive number for expense.");
}
}
function updateBudgetDisplay() {
const totalIncomeElement = document.getElementById("total-income");
const totalExpensesElement = document.getElementById("total-expenses");
const remainingBudgetElement = document.getElementById("remaining-budget");
totalIncomeElement.textContent = totalIncome.toFixed(2);
totalExpensesElement.textContent = totalExpenses.toFixed(2);
const remainingBudget = totalIncome - totalExpenses;
remainingBudgetElement.textContent = remainingBudget.toFixed(2);
}