## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# 5.18. Calculating the Loan (Mortgage) You Can Afford

## Problem

You want to calculate how large a loan you can get assuming you make a given monthly payment.

## Solution

Calculate the present value of a series of equal future periodic payments using the custom `Math.PV( )` function.

## Discussion

Suppose you are a bank with a big pile of money and you want to lend it to someone to earn some interest. The borrower might come to you and say, “I can pay you back \$1,000 per month for 30 years. How much money will you loan me?” The answer requires you to calculate how much the money that you will receive at various times in the future (as a series of periodic payments) is worth today.

The following custom `Math.PV( )` function returns the same value as the one obtained in Microsoft Excel using the Insert Function Financial PV formula. You can add this to your `Math.as` file for easy inclusion in other projects.

```Math.PV = function (i, n, PMT) {
// `i`   = periodic interest rate
// `n`   = number of payment periods
// `PMT` = periodic payment

// Present value compounded over `n` periods:
// `PV` = `PMT` * ( (`multiplier`-1)/(`i`*`multiplier`))
// where `multiplier` = `Math.pow` ((1 + `i`), `n`)

multiplier = Math.pow ((1 + i), n);
PV = PMT * (multiplier-1) / (i*multiplier);
return PV;
};```

You can use the custom `Math.PV( )` method as follows:

```// How much will the bank loan me if I can pay \$1000/month for 30 years (6% rate)?
trace (Math.PV(.06/12, 30*12, 1000));```