-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
103 lines (83 loc) · 2.8 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
const loanAmountInput = document.querySelector(".loan-amount");
const interestRateInput = document.querySelector(".interest-rate");
const loanTenureInput = document.querySelector(".loan-tenure");
const loanEMIValue = document.querySelector(".loan-emi .value");
const totalInterestValue = document.querySelector(".total-interest .value");
const totalAmountValue = document.querySelector(".total-amount .value");
const calculateBtn = document.querySelector(".calculate-btn");
// convert to number
let loanAmount = parseFloat(loanAmountInput.value);
let interestRate = parseFloat(interestRateInput.value);
let loanTenure = parseFloat(loanTenureInput.value);
// calculate interest for month
let interest = interestRate / 12 / 100;
const checkValues = () => {
let loanAmountValue = loanAmountInput.value;
let interestRateValue = interestRateInput.value;
let loanTenureValue = loanTenureInput.value;
let regexNumber = /^[0-9]+$/;
if (!loanAmountValue.match(regexNumber)) {
loanAmountInput.value = "10000";
}
if (!loanTenureValue.match(regexNumber)) {
loanTenureInput.value = "12";
}
let regexDecimalNumber = /^(\d*\.)?\d+$/;
if (!interestRateValue.match(regexDecimalNumber)) {
interestRateInput.value = "7.5";
}
};
let myChart;
const displayChart = (totalInterestAmount) => {
const ctx = document.getElementById("myChart").getContext("2d");
myChart = new Chart(ctx, {
type: "pie",
data: {
labels: ["Tổng tiền lãi", "Số tiền vay"],
datasets: [
{
data: [totalInterestAmount, loanAmount],
backgroundColor: ["#e63946", "#14213d"],
borderWidth: 0,
},
],
},
});
};
const updateChart = (totalInterestAmount) => {
myChart.data.datasets[0].data[0] = totalInterestAmount;
myChart.data.datasets[0].data[1] = loanAmount;
myChart.update();
};
const calculateEMI = () => {
let emi =
(loanAmount * interest * Math.pow(1 + interest, loanTenure)) /
(Math.pow(1 + interest, loanTenure) - 1);
return emi;
};
const updateData = (emi) => {
loanEMIValue.innerHTML = Math.round(emi);
let totalAmount = Math.round(loanTenure * emi);
totalAmountValue.innerHTML = totalAmount;
let totalInterestAmount = Math.round(totalAmount - loanAmount);
totalInterestValue.innerHTML = totalInterestAmount;
if (myChart) {
updateChart(totalInterestAmount);
} else {
displayChart(totalInterestAmount);
}
};
// get new values after change input value
const refreshInputValues = () => {
loanAmount = parseFloat(loanAmountInput.value);
interestRate = parseFloat(interestRateInput.value);
loanTenure = parseFloat(loanTenureInput.value);
interest = interestRate / 12 / 100;
};
const init = () => {
refreshInputValues();
let emi = calculateEMI();
updateData(emi);
};
init();
calculateBtn.addEventListener("click", init);