Skip to content

Instantly share code, notes, and snippets.

@phillro
Last active November 14, 2016 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillro/077321020e2af1f0aa3863f11d81e70b to your computer and use it in GitHub Desktop.
Save phillro/077321020e2af1f0aa3863f11d81e70b to your computer and use it in GitHub Desktop.
function calculatePaymentForPrequalifiedOffer(requestedAmount, loanOffer) {
function calcPmnt(amount, apr, months){
var interest = apr / 100 / 12;
var x = Math.pow(1 + interest, months);
var monthly = (requestedAmount*x*interest)/(x-1);
return Math.round(monthly*100)/100;
}
var out = {};
var months
switch(loanOffer.termUnit){
case "day" : months = (loanOffer.termLength / 365) * 12; break;
case "month" : months = loanOffer.termLength; break;
case "year" : months = loanOffer.termLength * 12; break;
}
//If its a static offer, the rate will be a range, so the monthly payment has to be a range.
if(loanOffer.maxApr && loanOffer.minApr){
out.maxMonthlyPayment = calcPmnt(requestedAmount, loanOffer.maxApr, months);
out.minMonthlyPayment = calcPmnt(requestedAmount, loanOffer.minApr, months);
} else {
out.monthlyPayment = calcPmnt(requestedAmount, loanOffer.maxApr, months);
}
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment