-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
89 lines (75 loc) · 2.34 KB
/
app.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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// ID SELECTING
const incomefield = document.getElementById("income_field");
const foodField = document.getElementById("food_field");
const rentField = document.getElementById("rent_field");
const clothesField = document.getElementById("clothes_field");
const calculateBtn = document.getElementById("calculateBtn");
const balanceId = document.getElementById("balanceId");
const totalExpenses = document.getElementById("total-expenses");
const saveField = document.getElementById("save_field");
const saveBtn = document.getElementById("saveBtn");
const savingAmount = document.getElementById("saving-amount");
const remainingBalanace = document.getElementById("remaining-balance");
// TOTAL EXPENSES
calculateBtn.addEventListener("click", function () {
isEmptyError();
handleError();
addition();
totalBalance();
});
// Addition Function
function addition() {
const foodAmount = parseInt(foodField.value);
const rentAmount = parseInt(rentField.value);
const clothesAmount = parseInt(clothesField.value);
const TotalExpense = foodAmount + rentAmount + clothesAmount;
totalExpenses.innerText = TotalExpense.toFixed(0);
}
// BALANCE
function totalBalance() {
const income = parseInt(incomefield.value);
const totalExpensesValue = parseInt(totalExpenses.innerText);
const balance = income - totalExpensesValue;
balanceId.innerText = balance.toFixed(0);
}
// Saving Function Click
saveBtn.addEventListener("click", function () {
handleError();
savePrice();
});
// SAVING
function savePrice() {
var numVal1 = parseInt(balanceId.innerText);
var numVal2 = parseInt(saveField.value);
var totalValue = numVal1 * (numVal2 / 100);
savingAmount.innerText = totalValue.toFixed(0);
remainingBalanace.innerText = balanceId.innerText - savingAmount.innerText;
}
/* ERROR HANDLING */
function isEmptyError() {
if (incomefield.value === "") {
alert("Field Must be not Empty");
return;
}
}
function handleError() {
if (
(incomefield.value ||
foodField.value ||
rentField.value ||
clothesField.value ||
saveField.valu) < 0
) {
alert("Please Insert only Positve Number");
return;
} else if (
isNaN(incomefield.value) ||
isNaN(foodField.value) ||
isNaN(rentField.value) ||
isNaN(clothesField.value) ||
isNaN(saveField.value) === true
) {
alert("Please insert a number");
return;
}
}