基于可扩展策略的房贷计算器

最近重新看《程序员的个人财富课》,其中讲到了程序员的能力可以用在很多方面,比如可以算一下如何买房最划算,并给了一个简单的房贷计算器的算法,我对这个算法进行了一定的扩展,直接上源码。

如果感兴趣想要体验,可以关注公众号后台回复房贷计算器,又一个将数据可视化后的demo。

源码

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// 等额本息
function calculateRepaymentBX(totalLoan, year, interestRate) {
// 将利率转换为月利率
const monthlyInterestRate = parseFloat(interestRate) / 12;
const loanTerm = year * 12;
const totalLoanByMillion = parseFloat(totalLoan);
// 计算每个月的还款额
const monthlyPayment = (totalLoanByMillion * monthlyInterestRate * Math.pow(1 + monthlyInterestRate, loanTerm)) / (Math.pow(1 + monthlyInterestRate, loanTerm) - 1);
// 计算总共的还款额
const totalRepayment = monthlyPayment * loanTerm;
// 初始化结果数组
const result = [];
let balance = totalLoanByMillion;
// 计算每个月的本金和利息
for (let i = 1; i <= loanTerm; i++) {
const monthlyInterest = balance * monthlyInterestRate;
const monthlyPrincipal = monthlyPayment - monthlyInterest;
balance -= monthlyPrincipal;
result.push({
month: i,
monthlyPayment: monthlyPayment,
monthlyPrincipal: monthlyPrincipal,
monthlyInterest: monthlyInterest,
balance: Math.max(0, balance)
});
}
// 返回结果
return {
totalRepayment: totalRepayment,
result: result
};
}

// 等额本金
function calculateRepaymentBJ(totalLoan, year, interestRate) {
// 将利率转换为月利率
const monthlyInterestRate = parseFloat(interestRate) / 12;
const loanTerm = year * 12;
const totalLoanByMillion = parseFloat(totalLoan);
// 计算每个月的本金
const monthlyPrincipal = parseFloat(totalLoanByMillion) / loanTerm;
// 初始化结果数组
const result = [];
// 计算每个月的还款额和利息
let balance = totalLoanByMillion;
let monthlyPayment;
let monthlyInterest;
for (let i = 1; i <= loanTerm; i++) {
monthlyInterest = balance * monthlyInterestRate;
monthlyPayment = monthlyPrincipal + monthlyInterest;
balance -= monthlyPrincipal;
result.push({
month: i,
monthlyPayment: monthlyPayment,
monthlyPrincipal: monthlyPrincipal,
monthlyInterest: monthlyInterest,
balance: Math.max(0, balance)
});
}
// 计算总共的还款额
const totalRepayment = totalLoanByMillion + result.reduce((total, current) => total + current.monthlyInterest, 0);
// 返回结果
return {
totalRepayment: totalRepayment,
result: result,
};
}

const LoanType = {
BJ: 'BJ',
BX: 'BX',
}

const calculateRepayment = {
[LoanType.BJ]: calculateRepaymentBJ,
[LoanType.BX]: calculateRepaymentBX,
}

function calculate(loadType, syLoan, gjjLoan, syYear, gjjYear, syInterestRate, gjjInterestRate) {
const syRepayment = calculateRepayment[loadType](syLoan, syYear, syInterestRate)
const gjjRepayment = calculateRepayment[loadType](gjjLoan, gjjYear, gjjInterestRate);

const [shortRepayment, longRepayment] = [syRepayment.result, gjjRepayment.result].sort((a, b) => a.length - b.length);

const mergedRepayment = longRepayment.map((longItem, i) => {
if (i < longRepayment.length) {
const shortItem = shortRepayment[i];
return {
month: longItem.month,
monthlyPayment: longItem.monthlyPayment + shortItem.monthlyPayment,
monthlyPrincipal: longItem.monthlyPrincipal + shortItem.monthlyPrincipal,
monthlyInterest: longItem.monthlyInterest + shortItem.monthlyInterest,
balance: longItem.balance + shortItem.balance
}
} else {
return {
month: longItem.month,
monthlyPayment: longItem.monthlyPayment,
monthlyPrincipal: longItem.monthlyPrincipal,
monthlyInterest: longItem.monthlyInterest,
balance: longItem.balance
}
}
})

return {
totalRepayment: syRepayment.totalRepayment + gjjRepayment.totalRepayment,
result: mergedRepayment
}
}

const strategies = [
{
name: '先租后买,全款买(排除理财与房价变动因素)',
execute: ({totalPrice, firstPay, currentPrincipal, gjjLoan, income, outcome, rent, syYear, gjjYear, syInterestRate, gjjInterestRate}) => {
const need = Math.ceil(parseFloat(totalPrice - currentPrincipal) / (income - outcome - rent));
return {
totalNeed: need,
firstPayNeed: need,
totalPayment: totalPrice + need * rent,
detail: new Array(need).fill(0).map((_, i) => ({ month: i + 1, monthlyPayment: rent, monthlyPrincipal: rent, monthlyInterest: 0, balance: totalPrice }))
.concat([{ month: need + 1, monthlyPayment: totalPrice, monthlyPrincipal: totalPrice, monthlyInterest: 0, balance: 0 }])
}
}
},
{
name: '等额本金贷款买',
execute: ({totalPrice, firstPay, currentPrincipal, gjjLoan, income, outcome, rent, syYear, gjjYear, syInterestRate, gjjInterestRate}) => {
const repayment = calculate('BJ', totalPrice - firstPay - gjjLoan, gjjLoan, syYear, gjjYear, syInterestRate, gjjInterestRate);
const { result, totalRepayment } = repayment;
const firstPayNeed = Math.max(0, firstPay - currentPrincipal) / (income - outcome - rent);
return {
totalNeed: firstPayNeed + Math.max(syYear, gjjYear) * 12,
firstPayNeed,
totalPayment: totalRepayment+ firstPayNeed * rent + firstPay,
detail: new Array(firstPayNeed).fill(0).map((_, i) => ({ month: i + 1, monthlyPayment: rent, monthlyPrincipal: rent, monthlyInterest: 0, balance: totalPrice }))
.concat(result)
}
}
},
{
name: '等额本息贷款买',
execute: ({totalPrice, firstPay, currentPrincipal, gjjLoan, income, outcome, rent, syYear, gjjYear, syInterestRate, gjjInterestRate}) => {
const repayment = calculate('BX', totalPrice - firstPay - gjjLoan, gjjLoan, syYear, gjjYear, syInterestRate, gjjInterestRate);
const { result, totalRepayment } = repayment;
const firstPayNeed = Math.max(0, firstPay - currentPrincipal) / (income - outcome - rent);
return {
totalNeed: firstPayNeed + Math.max(syYear, gjjYear) * 12,
firstPayNeed,
totalPayment: totalRepayment+ firstPayNeed * rent + firstPay,
detail: new Array(firstPayNeed).fill(0).map((_, i) => ({ month: i + 1, monthlyPayment: rent, monthlyPrincipal: rent, monthlyInterest: 0, balance: totalPrice }))
.concat(result)
}
}
},
{
name: '等额本金贷款买,能提前还款则提前还款(不考虑提前还款的影响)',
execute: ({totalPrice, firstPay, currentPrincipal, gjjLoan, income, outcome, rent, syYear, gjjYear, syInterestRate, gjjInterestRate}) => {
const repayment = calculate('BJ', totalPrice - firstPay - gjjLoan, gjjLoan, syYear, gjjYear, syInterestRate, gjjInterestRate);
const { result } = repayment;
let total = Math.max(0, currentPrincipal - firstPay);
let loanNeed = 0;
for(loanNeed = 0; loanNeed < result.length; loanNeed++) {
total += income - outcome - result[loanNeed].monthlyPayment
if (total >= result[loanNeed].balance) {
break;
}
}
const firstPayNeed = Math.max(0, firstPay - currentPrincipal) / (income - outcome - rent);
return {
totalNeed: loanNeed + firstPayNeed,
firstPayNeed,
totalPayment: result.slice(0, loanNeed).map(_ => _.monthlyPayment).reduce((total, cur) => total + cur, 0) + result[loanNeed].balance + firstPay,
detail: new Array(firstPayNeed).fill(0).map((_, i) => ({ month: i + 1, monthlyPayment: rent, monthlyPrincipal: rent, monthlyInterest: 0, balance: totalPrice }))
.concat(result.slice(0, loanNeed).map((_) => ({ ..._, month: firstPayNeed + _.month })))
.concat([{ month: firstPayNeed + loanNeed + 1, monthlyPrincipal: result[loanNeed].balance, monthlyInterest: 0, monthlyPayment: result[loanNeed].balance, balance: 0 }])
}
}
},
{
name: '等额本息贷款买,能提前还款则提前还款(不考虑提前还款的影响)',
execute: ({totalPrice, firstPay, currentPrincipal, gjjLoan, income, outcome, rent, syYear, gjjYear, syInterestRate, gjjInterestRate}) => {
const repayment = calculate('BX', totalPrice - firstPay - gjjLoan, gjjLoan, syYear, gjjYear, syInterestRate, gjjInterestRate);
const { result } = repayment;
let total = Math.max(0, currentPrincipal - firstPay);
let loanNeed = 0;
for(loanNeed = 0; loanNeed < result.length; loanNeed++) {
total += income - outcome - result[loanNeed].monthlyPayment
if (total >= result[loanNeed].balance) {
break;
}
}
const firstPayNeed = Math.max(0, firstPay - currentPrincipal) / (income - outcome - rent);
return {
totalNeed: loanNeed + firstPayNeed,
firstPayNeed,
totalPayment: result.slice(0, loanNeed).map(_ => _.monthlyPayment).reduce((total, cur) => total + cur, 0) + result[loanNeed].balance + firstPay,
detail: new Array(firstPayNeed).fill(0).map((_, i) => ({ month: i + 1, monthlyPayment: rent, monthlyPrincipal: rent, monthlyInterest: 0, balance: totalPrice }))
.concat(result.slice(0, loanNeed).map((_) => ({ ..._, month: firstPayNeed + _.month })))
.concat([{ month: firstPayNeed + loanNeed + 1, monthlyPrincipal: result[loanNeed].balance, monthlyInterest: 0, monthlyPayment: result[loanNeed].balance, balance: 0 }])
}
}
},
]


function executeStrategies ({totalPrice, firstPay, currentPrincipal, gjjLoan, income, outcome, rent, syYear, gjjYear, syInterestRate, gjjInterestRate}) {
console.log(`房屋总价为${totalPrice}元,首付为:${firstPay}元,现有:${currentPrincipal}元,月收入为:${income}元,每个月租房支出为:${rent}元,其他消费为:${outcome}元`)
const result = [];
strategies.forEach(({name, execute}) => {
const repayment = execute({totalPrice, firstPay, currentPrincipal, gjjLoan, income, outcome, rent, syYear, gjjYear, syInterestRate, gjjInterestRate});
console.log(`策略:${name}, 需要时间为${repayment.totalNeed}个月, 住房总花费为${repayment.totalPayment}元`)
result.push({...repayment, strategyName: name});
})
return result;
}

executeStrategies({
totalPrice: 5000000,
firstPay: 1500000,
currentPrincipal: 1500000,
gjjLoan: 1000000,
income: 50000,
outcome: 5000,
rent: 3000,
syYear: 20,
gjjYear: 20,
syInterestRate: 0.043,
gjjInterestRate: 0.031
})

module.exports = {
executeStrategies
}