Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to calculate mortgage in javascript

I have formula for finding monthly payment with four fields

  1. Loan Amount
  2. Interest Rate
  3. Terms of loan
  4. Monthly Payment

Formula: Monthly Payment =Loan amount * ((1 + Interest rate per annum/100) ^ Term of loan) / Term of loan / 12

Now I want to find

  1. Loan Amount
  2. Interest Rate
  3. Terms of loan

if any of three fields are populated.

I have also formula for calculating loan amount based on interest rate, terms of loan and monthly payment.

Formula: Loan amount = Monthly Payment/ ((1 + Interest rate per annum/100) ^ Term of loan) * Term of loan * 12

But it does not calculating perfect figure.

Any one can give me these three formulas for calculating loan amount/interest rate/terms of loan (java script will be more appreciated)

like image 334
steve Avatar asked Jun 14 '13 05:06

steve


3 Answers

Here is mine,

the formula :

M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]

nerdWallet

Hope this helps in someway

var M; //monthly mortgage payment
var P = 400000; //principle / initial amount borrowed
var I = 3.5 / 100 / 12; //monthly interest rate
var N = 30 * 12; //number of payments months

//monthly mortgage payment
M = monthlyPayment(P, N, I);

console.log(M);

function monthlyPayment(p, n, i) {
  return p * i * (Math.pow(1 + i, n)) / (Math.pow(1 + i, n) - 1);
}
like image 194
shay Avatar answered Sep 30 '22 17:09

shay


var deno = (100 + Interest_rate_per_annum)/100;
var pdeno = Math.pow(deno, Term_of_Loan);
var loan_amount = (Monthly_payment * Term_of_Loan * 12) / pdeno;
like image 36
kishoredbn Avatar answered Sep 30 '22 16:09

kishoredbn


This is the exact same answer as @shay gave but with the variable names spelled out to make it easier for me to understand:

// totalPayments should be the total number of payments expected to be made for the life of the loan: years * 12
// interestRate: eg. 6.2% should be passed as 0.062
function getMortgagePayment(startingLoanAmount, totalPayments, interestRate)
{
    let interestRatePerMonth = interestRate / 12;
    return startingLoanAmount * interestRatePerMonth * (Math.pow(1 + interestRatePerMonth, totalPayments)) / (Math.pow(1 + interestRatePerMonth, totalPayments) - 1);
}
like image 25
John Langford Avatar answered Sep 30 '22 17:09

John Langford