-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmortgage.js
62 lines (48 loc) · 1.57 KB
/
mortgage.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
'use strict';
var money = require('./money');
function Mortgage(amount, interest, years) {
this.amount = amount;
this.interest = interest / 12;
this.months = years * 12;
}
Mortgage.prototype.fixedMonthlyPayment = function () {
var a = this.amount;
var i = this.interest;
var n = this.months;
var payment = a * ((i * Math.pow(1 + i, n)) / (Math.pow(1 + i, n) - 1));
return payment.toFixed(2);
};
Mortgage.prototype.remainingLoanBalance = function (afterMonths) {
var p = afterMonths;
var a = this.amount;
var i = this.interest;
var n = this.months;
var balance = a * ((Math.pow(1 + i, n) - Math.pow(1 + i, p)) / (Math.pow(1 + i, n) - 1));
return balance.toFixed(2);
};
// Returns a table of the following: payment, principal paid, interest paid, total interest, balance.
Mortgage.prototype.amortizationTable = function () {
var balance = this.amount;
var interest = this.interest;
var payment = this.fixedMonthlyPayment();
var totalInterestPaid = 0;
var totalPrincipalPaid = 0;
var table = [];
for (var i = 0; i < this.months; i++) {
// TODO: decimal rounding
var interestPaid = money.round(balance * interest);
var principalPaid = money.round(payment - interestPaid);
totalInterestPaid = money.round(totalInterestPaid + interestPaid);
totalPrincipalPaid = money.round(totalPrincipalPaid + principalPaid);
balance = money.round(balance - principalPaid);
table.push([
payment,
principalPaid,
interestPaid,
totalInterestPaid,
balance
]);
}
return table;
};
module.exports = Mortgage;