# Valuing a simple Annuity Due using Javascript

This post is about valuing a simple annuity due using Javascript programming. This post will be building off of a previous post Valuing a Simple Ordinary Annuity using Javascript since Annuity Due and Ordinary Annuities are very similar. To try out this calculator click the following link:

https://jsfiddle.net/Zerkon/fygkL78s/2/show/ Simple Annuity calculator modified to handle Annuity Due as well as Ordinary Annuity

What is an Annuity Due

Just like an Ordinary Annuity, an Annuity Due is a series of payments. The difference is that an ordinary annuity pays at the end of a payment period whereas annuity due is paid at the beginning. This means that the present value of each payment of an annuity due is different than an ordinary annuity. For example, we have an annual payment of \$100 and the prevailing interest rate is 5%.

For an annuity, due it’s $100/1.05^0 = 100$ since we are paid at the beginning of the year and for ordinary annuity it’s $100/1.05^1= 95.24$ since we are paid at the end of the year.

With that difference, let’s get coding.

HTML

For the layout for the controls, it will be almost exactly the same as in my previous post, however, I am adding an additional feature which will allow us to choose whether we want to evaluate the security as an annuity due or ordinary annuity.

```<tr>
<td>Annuity Type</td>
<td>
<select id="type">
<option value="ord"> Ordinary Annuity</option>
<option value="due">Annuity Due</option>
</select></td>
</tr>
```

This snippet of code allows the user to choose which type of annuity they want.

Here’s the full HTML, scroll down to get the Javascript part.

```</pre>
<h2>Simple Annuity Calculator</h2>
<table style="width:100%">
<tr>
<td>Annuity Type</td>
<td>
<select id="type">
<option value="ord"> Ordinary Annuity</option>
<option value="due">Annuity Due</option>
</select></td>
</tr>
<tr>
<td width="150px"> Payment Amount
<td>
<input id="payment"></tr>
<tr>
<td> Interest Rate (%)</td>
<td>
<input id="interest"></td>
</tr>
<tr>
<td> Frequency of Payments
<td>
<select id="period">
<option value="annually"> Annually</option>
<option value="semi annually"> Semi Annually</option>
<option value="quarterly"> Quarterly</option>
<option value="monthly"> Monthly</option>
<option value="bimonthly"> Bi Monthly </option>
<option value="weekly"> Weekly </option>
</select></tr>
<tr>
<td> Number of Years
<td>
<input id="years"></tr>
<tr>
<td></td>
<td>
<button type="button" onclick="myFunction()">Submit</button></td>
</tr>
</table>
<p id="demo"></p>

```

Javascript

The idea here is the same as with Ordinary annuities. Again I am making the assumption that the payments will be constant and unchanging. Thus I get the same mathematical equation as Ordinary Annuity,

Except I multiply in $1+r$ since we are discounting on period less.

As a result, I also add it to the code as follows.

```
//if the user choose and annuity due

if(annuitytype == "due") value = value * (1 + temp);

```

Remember the user gets to choose which type of annuity they want so the discount factor will only be multiplied in if the user explicitly chooses Annuity Due.

Here is the full Javascript Code:

```
function myFunction() {
var text;
var value = 0.0;

// gets the values from the inputs
var Payment = document.getElementById("payment").value;
var Interest = document.getElementById("interest").value;
var Years = document.getElementById("years").value;
var period = document.getElementById("period").value;
var lifespan = 1;

//lifespan variable is the frequency of payments per year
if (period == "annually") lifespan = 1;
if (period == "semi annually") lifespan = 2;
if (period == "quarterly") lifespan = 4;
if (period == "monthly") lifespan = 12;
if (period == "bimonthly") lifespan = 24;
if (period == "weekly") lifespan = 52;

// checks to see if these values are numbers
if (isNaN(Payment) || isNaN(Interest) || isNaN(Years)) {
text = "Input not valid";
}
else {
//calculates the present value of the annuity
var temp = Interest / (100*lifespan);
var temp2 = Math.pow((1 + temp), (lifespan * Years));
value = Payment * (1 - 1 / temp2) / (temp);

//if user chooses annuity due
if(annuitytype == "due") value = value * (1 + temp);

//grammatical cases for the output of the function
if(lifespan == 1 && Years == 1) text = "The Annuity pays \$" + Payment + ", once per year for one year, is worth \$" + value.toFixed(3);
else if(lifespan == 1) text = "The Annuity pays \$" + Payment + ", once per year for " + Years + " years, is worth \$" + value.toFixed(3);
else if(Years == 1) text = "The Annuity pays \$" + Payment + ", " + lifespan + " times per year for one year, is worth \$" + value.toFixed(3);
else text = "The Annuity pays \$" + Payment + ", " + lifespan + " times per year for " + Years + " years, is worth \$" + value.toFixed(3);
}

//outputs either an error message or the value of the annuity
document.getElementById("demo").innerHTML = text;
}
```

Thank you for reading my article, if you found it useful please like and share and if you have any questions please feel free to leave them in the comments – I may use your question for a further post.

Advertisements

## 5 thoughts on “Valuing a simple Annuity Due using Javascript”

1. outsourcedguru says:

It might be good to exercise the code in parallel with exercising an Excel spreadsheet to validate the numbers. Entering a payment amount of 1, interest of 1, frequency of annually and number of years of 1 should… result in something like \$1.01 if it’s a standard interest calculation. In Excel, that start/end-of-period boolean is entered as a 1 or 0 as I recall.

In Excel, the formula is entered: Rate: 1%, Nper: 1, Pmt: -1, Pv: 0, Type: 1, resulting in this formula as seen in the cell: =FV(1%,-1,0,1) and the value, as calculated, is \$1.01 as expected. Yours seems to calculate this as \$0.99 but perhaps I’m mis-interpreting what your calculator provides.

Liked by 1 person

1. finforthewinblog says:

I think you’re taking about the future value of money. This is calculating the price of an annuity by summing up the present values of the payments.

The annuity that you are talking about will pay me one dollar in at the end of a year and the prevailing market interest rate at 1%. The \$1 I get at the end of the year will be slightly less than the \$1 I have in my pocket right now. So reasonably, I will not pay more than \$0.99 today for a dollar i will receive at the end of the year.

If we take that to a series of payments, this is to calculate how much would be a fair price for me to pay to purchase that series of payments.

Like

1. outsourcedguru says:

In theory, they’re reciprocal functions, f(g(x)) where g() might be FV() and f() might be this annuity calculation and putting that same dollar in should result in a dollar after you’ve applied both functions.

Like

2. finforthewinblog says:

We are pricing an annuity … I’m not going to argue but I don’t think you’d be willing to pay someone \$1.01 now only to receive \$1 in the future. See this link for a pricing formula http://financeformulas.net/Annuity_Payment_Formula.html

Like

3. outsourcedguru says:

Yeah, I totally get it. Thanks.

Like