-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
39 lines (35 loc) · 1.53 KB
/
script.js
File metadata and controls
39 lines (35 loc) · 1.53 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
function calculateExpenses() {
var salary = parseFloat(document.getElementById("salary").value);
var electricity = parseFloat(document.getElementById("electricity").value);
var rent = parseFloat(document.getElementById("rent").value);
var food = parseFloat(document.getElementById("food").value);
var transport = parseFloat(document.getElementById("transport").value);
var other = parseFloat(document.getElementById("other").value);
if (isNaN(salary)) {
alert("Please enter a valid salary.");
return;
}
if (isNaN(electricity)) {
electricity = 0;
}
if (isNaN(rent)) {
rent = 0;
}
if (isNaN(food)) {
food = 0;
}
if (isNaN(transport)) {
transport = 0;
}
if (isNaN(other)) {
other = 0;
}
var totalExpenses = electricity + rent + food + transport + other;
var remainingSalary = salary - totalExpenses;
document.getElementById("electricity-expense").textContent = "Expense of electricity is " + electricity;
document.getElementById("rent-expense").textContent = "Expense of rent is " + rent;
document.getElementById("food-expense").textContent = "Expense of food is " + food;
document.getElementById("transport-expense").textContent = "Expense of transport is " + transport;
document.getElementById("other-expense").textContent = "Expense of other is " + other;
document.getElementById("remaining-salary").textContent = "Total Expenses subtracted from the salary is remaining as " + remainingSalary;
}