SOURCE

//等额本息还款法
function debx(l, yr, y) {
    //贷款总额=l,年利率=yr,还款年数=y

    var mr = yr / 12; //月利率计算
    var m = y * 12; //还款月数计算

    var loanAmount = l; //贷款总额
    var month = m; //还款月数
    var monthlyReturn; //每月还款金额
    var totalInterest; //总支付利息
    var total; //本息合计

    monthlyReturn = (l * mr * Math.pow((1 + mr), m)) / (Math.pow((1 + mr), m) - 1);
    totalInterest = (monthlyReturn * 360) - l;
    total = monthlyReturn * 360;

    var result = new Object();
    result.loanAmount = loanAmount; //贷款总额
    result.month = month; //还款月数
    result.monthlyReturn = monthlyReturn.toFixed(2); //每月还款
    result.totalInterest = totalInterest.toFixed(2); //总支付利息
    result.total = total.toFixed(2); //本息合计

    return result;

}

//等额本金还款法
function debj(l, yr, y) {
    //贷款总额=l,年利率=yr,还款年数=y

    var mr = yr / 12; //月利率
    var m = y * 12; //还款月数

    var loanAmount = l; //贷款总额
    var month = m; //还款月数
    var monthlyAmount = l / m; //每月本金

    var firstReturn; //首月还款
    var secondReturn; //次月还款
    var monthlyMinus; //每月递减
    var totalInterest; //总支付利息
    var total; //本息合计

    firstReturn = monthlyAmount + l * mr;
    secondReturn = monthlyAmount + (l - monthlyAmount) * mr;
    monthlyMinus = firstReturn - secondReturn;

    totalInterest = (m + 1) * l * mr / 2;
    total = l + totalInterest;

    var result = new Object();
    result.loanAmount = loanAmount; //贷款总额
    result.month = month; //还款月数
    result.firstReturn = firstReturn.toFixed(2); //首月还款
    result.monthlyMinus = monthlyMinus.toFixed(2); //每月递减
    result.totalInterest = totalInterest.toFixed(2); //总支付利息
    result.total = total.toFixed(2); //本息合计

    return result;

}

//还款方式判断
function loanCalculator(obj){

    var result = new Object();  //结果对象

    //method:商业贷款0,公积金贷款1,组合贷款2
    if(obj.method === "0"){

        var bx = debx(obj.businessLoan, obj.businessLoanRate, obj.year);
        var bj = debj(obj.businessLoan, obj.businessLoanRate, obj.year);

        result.x = JSON.stringify(bx);
        result.j = JSON.stringify(bj);

        return result;

    } else if(obj.method === "1"){
        
        var hx = debx(obj.hpfLoan, obj.hpfLoanRate, obj.year);
        var hj = debj(obj.hpfLoan, obj.hpfLoanRate, obj.year);

        result.x = JSON.stringify(hx);
        result.j = JSON.stringify(hj);

        return result;

    } else if(obj.method === "2") {
        
        var bx = debx(obj.businessLoan, obj.businessLoanRate, obj.year);
        var bj = debj(obj.businessLoan, obj.businessLoanRate, obj.year);
        var hx = debx(obj.hpfLoan, obj.hpfLoanRate, obj.year);
        var hj = debj(obj.hpfLoan, obj.hpfLoanRate, obj.year);

        var loanAmount = bx.loanAmount + hx.loanAmount;
        var month = bx.month;

        var xMonthlyReturn = bx.monthlyReturn + hx.monthlyReturn;
        var xTotalInterest = bx.totalInterest + hx.totalInterest;
        var xTotal = bx.total + hx.total;

        var jFirstReturn = bj.firstReturn +hj.firstReturn;
        var jMonthlyMinus = bj.monthlyMinus + hj.monthlyMinus;
        var jTotalInterest = bj.totalInterest + hj.totalInterest;
        var jTotal = bj.total + hj.total;

        result = {
            "x": {
                "loanAmount": loanAmount,
                "month": month,
                "monthlyReturn": xMonthlyReturn,
                "totalInterest": xTotalInterest,
                "total": xTotal
            },
            "j": {
                "loanAmount": loanAmount,
                "month": month,
                "firstReturn": jFirstReturn,
                "monthlyMinus": jMonthlyMinus,
                "totalInterest": jTotalInterest,
                "total": jTotal
            }
        }

        return result;

    }

}

var submit = {
    "method": "2",
    "year": 30,
    "businessLoan": 500000,
    "businessLoanRate": 0.049,
    "hpfLoan": 500000,
    "hpfLoanRate": 0.0325
};

var d = loanCalculator(submit);
console.log(d);
console 命令行工具 X clear

                    
>
console