const result = equalInstallmentRecursive(400000, 120,0.035, )
function equalInstallmentRecursive(amount, term, annualRate) {
const monthRate = annualRate / 12
const total =
(amount * monthRate * Math.pow(1 + monthRate, term)) /
(Math.pow(1 + monthRate, term) - 1)
const details = []
// 递归主体
function build(balance, leftTerm, period) {
if (leftTerm <= 0 || balance <= 0) return
const interestRaw = balance * monthRate
const interest = +interestRaw.toFixed(2)
const fixedPayment = +total.toFixed(2)
const fixedPrincipal = +(fixedPayment - interest).toFixed(2)
details.push({
period,
principal: fixedPrincipal,
interest: interest,
total: fixedPayment,
})
const nextBalance = +(balance - fixedPrincipal).toFixed(2)
build(nextBalance, leftTerm - 1, period + 1)
}
build(amount, term, 1)
return { details }
}