Home > Articles > Web Development > Ajax and JavaScript

  • Print
  • + Share This
From the author of

Calculating Required Investment Deposits

A slightly different problem is when a person has a financial goal in mind and needs to know what it will take to get there. For example, if a couple thinks they'll need $60,000 for their child's education in 18 years, what do they have to put aside every year (assuming 8% annual interest)? Here's the general formula:

PMT = FV x IR / ((1 + IR)NP – 1)

Here's how it looks translated into JavaScript:

PMT = FV * IR / (Math.pow(1 + IR, NP) - 1)

Listing 4 shows you how to calculate the specific example given above.

Listing 4: Calculating Required Investment Deposits

<script language="JavaScript" type="text/javascript">
<!--

function required_deposits(FV, IR, NP) {
  var PMT = FV * IR / (Math.pow(1 + IR, NP) - 1)
  return round_decimals(PMT, 2)
}

function round_decimals(original_number, decimals) {
  var result1 = original_number * Math.pow(10, decimals)
  var result2 = Math.round(result1)
  var result3 = result2 / Math.pow(10, decimals)
  return (result3)
}

var future_value = 60000
var interest_rate = 0.08
var investment_term = 18
var annual_deposit = required_deposits(future_value, interest_rate, 
                    investment_term)

alert("Future value:\t$" + future_value + "\n" +
   "Annual interest rate:\t" + interest_rate * 100 + "%\n" +
   "Investment term:\t" + investment_term + " years\n\n" +
   "Annual deposit:\t$" + annual_deposit)

//-->
</script>
  • + Share This
  • 🔖 Save To Your Account