-
Notifications
You must be signed in to change notification settings - Fork 0
/
FinancialCalculator.js
103 lines (84 loc) · 4.3 KB
/
FinancialCalculator.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
document.addEventListener("DOMContentLoaded", () => {
const creditAmount = document.getElementById("creditAmount"); // сумма кредита
const interestRate = document.getElementById("interestRate"); // процентная ставка
const creditTerm = document.getElementById("creditTerm"); // срок кредита
const familyIncome = document.getElementById("familyIncome"); // доход семьи
const numberOfFamilyMembers = document.getElementById("numberOfFamilyMembers"); // число членов семьи
const communalPayments = document.getElementById("communalPayments"); // ком. платежи
const gettingResult = document.getElementById("gettingResult"); // получить результат
const clearingResult = document.getElementById("clearingResult"); // отчистить результат
let result = document.createElement("div");
result.id = "result";
gettingResult.addEventListener("click", () => {
emptinessCheck();
});
clearingResult.addEventListener("click", () => {
clearResult();
});
function emptinessCheck() {
if (Number(creditAmount.value) === 0 || Number(interestRate.value) === 0 ||
Number(creditTerm.value) === 0 || Number(familyIncome.value) === 0 ||
Number(numberOfFamilyMembers.value) === 0 || Number(communalPayments.value) === 0) {
result.innerHTML = "<h3 id='warning'>Некорректные данные</h3>";
document.body.append(result);
return 0;
}
creditCheck();
}
function creditCheck() {
removeResultDiv();
const livingWage = 12284; // прожиточный минимум по Новосибирской области
const familyBudget = Number(familyIncome.value) - (Number(communalPayments.value) + Number(livingWage * numberOfFamilyMembers.value));
const creditBody = Number(creditAmount.value) / Number(creditTerm.value);
if (familyBudget < creditBody) {
result.innerHTML = "<h3 id='warning'>К сожалению, Вы не можете взять кредит на данную сумму,</h3>" +
"<h3 id='warning'>так как кредитный платеж превышает бюджет семьи</h3>";
document.body.append(result);
} else {
if (creditAmount.value !== "" && interestRate.value !== "" && creditTerm.value !== "" &&
familyIncome.value !== "" && numberOfFamilyMembers.value !== "" && communalPayments.value !== "") {
getResult();
}
}
}
function getResult() {
removeResultDiv();
let amount = Number(creditAmount.value);
let rate = Number(interestRate.value) / 100 / 12;
let period = Number(creditTerm.value);
let differentiatedPayment = 0;
let percents = 0;
let remainingAmount = amount;
let balanceOwed = Number(amount / period);
let str = "<table><tr id='columnHeader'><th id=\"number\">№</th><th>Платеж</th><th>Проценты</th><th>Тело кредита</th><th>Остаток</th></tr>";
for (let i = 0; i < period; i++) {
percents = Number(remainingAmount * rate);
remainingAmount = Number(remainingAmount - balanceOwed);
differentiatedPayment = Number(percents) + Number(balanceOwed);
str += `<tr>
<td id='number'>${i + 1}</td>
<td>${differentiatedPayment.toFixed(2)}</td>
<td>${percents.toFixed(2)}</td>
<td>${balanceOwed.toFixed(2)}</td>
<td>${remainingAmount.toFixed(2)}</td>
</tr>`;
}
str += "</table>";
result.innerHTML = str;
document.body.append(result);
}
function clearResult() {
creditAmount.value = "";
interestRate.value = "";
creditTerm.value = "";
familyIncome.value = "";
numberOfFamilyMembers.value = "";
communalPayments.value = "";
document.getElementById("result").remove();
}
function removeResultDiv() {
if (document.getElementById("result")) {
document.getElementById("result").remove();
}
}
});