-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
132 lines (120 loc) · 5.54 KB
/
index.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
const defaultPrice = 500000;
const defaultDownPayment = 100000;
const defaultInterestRate = 0.05;
const defaultMonths = 360;
const defaultTaxRate = 0.0126;
const defaultInsuranceRate = 0.0014;
const defaultMortgageInsuranceRate = 0.011;
const defaultMortgageInsuranceEnabled = true;
const defaultMortgageInsuranceThreshold = 0.2;
const defaultAdditionalPrincipalPayment = 0;
class MortgageCalculator {
constructor() {
this.totalPrice = defaultPrice;
this.downPayment = defaultDownPayment;
this.interestRate = defaultInterestRate;
this.months = defaultMonths;
this.taxRate = defaultTaxRate;
this.insuranceRate = defaultInsuranceRate;
this.mortgageInsuranceRate = defaultMortgageInsuranceRate;
this.mortgageInsuranceEnabled = defaultMortgageInsuranceEnabled;
this.mortgageInsuranceThreshold = defaultMortgageInsuranceThreshold;
this.additionalPrincipalPayment = defaultAdditionalPrincipalPayment;
}
calculatePayment() {
let loanAmount = this.totalPrice - this.downPayment;
let paymentSchedule = MortgageCalculator.calculatePaymentSchedule(loanAmount, this.interestRate, this.months, this.additionalPrincipalPayment);
let piPayment = paymentSchedule.length ? paymentSchedule[0].totalPayment : 0;
let downPaymentPercentage = this.downPayment / this.totalPrice;
let mortgageInsurance = 0;
if (this.mortgageInsuranceEnabled && downPaymentPercentage < this.mortgageInsuranceThreshold) {
mortgageInsurance = (loanAmount * this.mortgageInsuranceRate) / 12;
}
let propertyTax = (this.totalPrice * this.taxRate) / 12;
let homeOwnerInsurance = MortgageCalculator.roundPenny((this.totalPrice * this.insuranceRate) / 12);
return {
loanAmount: loanAmount,
principalAndInterest: piPayment,
tax: propertyTax,
insurance: homeOwnerInsurance,
total: piPayment + propertyTax + homeOwnerInsurance + mortgageInsurance,
termMonths: this.months,
paymentSchedule: paymentSchedule,
mortgageInsurance: mortgageInsurance
};
}
static calculatePaymentSchedule(loanAmount, annualRate, termMonths, additionalPrincipalPayments = 0) {
const monthlyRate = annualRate / 12;
const monthlyPayment = MortgageCalculator.calculateMonthlyPIPayment(loanAmount, annualRate, termMonths);
let principal = MortgageCalculator.roundPenny(loanAmount);
let payments = [];
let totalInterest = 0;
let totalPayments = 0;
let i = 0;
while (principal > 0 && i < termMonths) {
let interestPayment = MortgageCalculator.roundPenny(principal * monthlyRate);
let principalPayment = MortgageCalculator.roundPenny(monthlyPayment - interestPayment + additionalPrincipalPayments);
if (principal > principalPayment) {
principal = MortgageCalculator.roundPenny(principal - principalPayment);
}
else {
principalPayment = principal;
principal = 0;
}
let totalPayment = interestPayment + principalPayment;
totalInterest += interestPayment;
totalPayments += totalPayment;
payments[i] = {
count: i+1,
interestPayment: interestPayment,
totalInterest: totalInterest,
principalPayment: principalPayment,
totalPayment: totalPayment,
totalPayments: totalPayments,
balance: principal
};
i++;
}
return payments;
}
static calculateMonthlyPIPayment(loanAmount, annualRate, termMonths) {
let monthlyRate = annualRate / 12;
let payment = (monthlyRate * loanAmount * Math.pow(1 + monthlyRate, termMonths)) / (Math.pow(1 + monthlyRate, termMonths) - 1);
return this.nextPenny(payment);
}
static roundPenny(value) {
return Math.round(value * 100)/100;
}
static nextPenny(value) {
return Math.ceil(value * 100)/100;
}
}
const _calc = new MortgageCalculator();
module.exports = {
createMortgageCalculator: function(){
return new MortgageCalculator();
},
calculatePayment: function(totalPrice = defaultPrice,
downPayment = defaultDownPayment,
interestRate = defaultInterestRate,
months = defaultMonths,
taxRate = defaultTaxRate,
insuranceRate = defaultInsuranceRate,
mortgageInsuranceRate = defaultMortgageInsuranceRate,
mortgageInsuranceEnabled = defaultMortgageInsuranceEnabled,
mortgageInsuranceThreshold = defaultMortgageInsuranceThreshold,
additionalPrincipalPayment = defaultAdditionalPrincipalPayment) {
_calc.totalPrice = totalPrice;
_calc.downPayment = downPayment;
_calc.interestRate = interestRate;
_calc.months = months;
_calc.taxRate = taxRate;
_calc.insuranceRate = insuranceRate;
_calc.mortgageInsuranceRate = mortgageInsuranceRate;
_calc.mortgageInsuranceEnabled = mortgageInsuranceEnabled;
_calc.mortgageInsuranceThreshold = mortgageInsuranceThreshold;
_calc.additionalPrincipalPayment = additionalPrincipalPayment;
return _calc.calculatePayment();
},
nextPenny: MortgageCalculator.nextPenny
};