function debx(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) {
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();
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
};
result.j = {
"loanAmount": loanAmount,
"month": month,
"firstReturn": jFirstReturn,
"monthlyMinus": jMonthlyMinus,
"totalInterest": jTotalInterest,
"total": jTotal
}
result.x = JSON.stringify(result.x);
result.j = JSON.stringify(result.j);
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