-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
149 lines (122 loc) · 4.39 KB
/
main.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
function myFunction() {
var element = document.body;
element.classList.toggle("dark-mode");
}
const sun = document.querySelector(".sun");
const moon = document.querySelector(".moon");
const button = document.querySelector(".start");
button.addEventListener("click", () => {
sun.classList.toggle("visible");
moon.classList.toggle("visible");
});
const loanAmountInput = document.querySelector(".loan-amount");
const interestRateInput = document.querySelector(".interest-rate");
const loanTenureInput = document.querySelector(".loan-tenure");
const processingFeeInput = document.querySelector(".processing-fee");
const downPaymentInput = document.querySelector(".down-payment");
const loanEMIValue = document.querySelector(".loan-emi .value");
const totalInterestValue = document.querySelector(".total-interest .value");
const totalAmountValue = document.querySelector(".total-amount .value");
const totalDownPaymentValue = document.querySelector(
".total-down-payment .value"
);
const calculateBtn = document.querySelector(".calculate-btn");
let loanAmount = parseFloat(loanAmountInput.value);
let newAmount = 0;
for (let i = 0; i < 1; i++) {
newAmount = loanAmount;
}
let interestRate = parseFloat(interestRateInput.value);
let loanTenure = parseFloat(loanTenureInput.value);
let processingFee = parseFloat(processingFeeInput.value);
let downPayment = parseFloat(downPaymentInput.value);
let interest = interestRate / 12 / 100;
let processFee = processingFee / 100;
let myChart;
const checkValues = () => {
let loanAmountValue = loanAmountInput.value;
let interestRateValue = interestRateInput.value;
let loanTenureValue = loanTenureInput.value;
let processingFeeValue = processingFeeInput.value;
let downPaymentValue = downPaymentInput.value;
let regexNumber = /^[0-9]+$/;
if (!loanAmountValue.match(regexNumber)) {
loanAmountInput.value = "20000";
}
if (!loanTenureValue.match(regexNumber) || loanTenureValue > 360) {
loanTenureInput.value = "180";
}
if (!downPaymentValue.match(regexNumber) || downPaymentValue > newAmount) {
downPaymentInput.value = "2000";
}
let regexDecimalNumber = /^(\d*\.)?\d+$/;
if (!interestRateValue.match(regexDecimalNumber)) {
interestRateInput.value = "7.5";
}
if (!processingFeeValue.match(regexDecimalNumber)) {
processingFeeInput.value = "1.2";
}
};
const displayChart = (totalInterestPayableValue, processingFees) => {
const ctx = document.getElementById("myChart").getContext("2d");
myChart = new Chart(ctx, {
type: "pie",
data: {
labels: ["Total Interest", "Principal Loan Amount", "Processing Fees"],
datasets: [
{
data: [totalInterestPayableValue, loanAmount, processingFees],
backgroundColor: ["#e63946", "#050A30", "#ff7415"],
borderWidth: 0,
},
],
},
});
};
const updateChart = (totalInterestPayableValue, processingFees) => {
myChart.data.datasets[0].data[0] = totalInterestPayableValue;
myChart.data.datasets[0].data[1] = loanAmount;
myChart.data.datasets[0].data[2] = processingFees;
myChart.update();
};
const refreshInputValues = () => {
loanAmount = parseFloat(loanAmountInput.value);
interestRate = parseFloat(interestRateInput.value);
loanTenure = parseFloat(loanTenureInput.value);
processingFee = parseFloat(processingFeeInput.value);
downPayment = parseFloat(downPaymentInput.value);
interest = interestRate / 12 / 100;
processFee = processingFee / 100;
};
const calculateEMI = () => {
checkValues();
refreshInputValues();
loanAmount = loanAmount - downPayment;
let emi =
loanAmount *
interest *
(Math.pow(1 + interest, loanTenure) /
(Math.pow(1 + interest, loanTenure) - 1));
return emi;
};
const updateData = (emi) => {
loanEMIValue.innerHTML = Math.ceil(emi);
let processingFees = Math.ceil(loanAmount * processFee);
let totalDownPay = Math.ceil(downPayment + processingFees);
totalDownPaymentValue.innerHTML = totalDownPay;
let totalAmount = Math.ceil(loanTenure * emi);
totalAmountValue.innerHTML = totalAmount;
let totalInterestPayable = Math.ceil(totalAmount - loanAmount);
totalInterestValue.innerHTML = totalInterestPayable;
if (myChart) {
updateChart(totalInterestPayable, processingFees);
} else {
displayChart(totalInterestPayable, processingFees);
}
};
const init = () => {
let emi = calculateEMI();
updateData(emi);
};
init();
calculateBtn.addEventListener("click", init);