Introduction to Voice XML Part 5: Voice XML Meets Web 2.0

• Print

Voice XML Financial Calculator

With a little JavaScript under our belts, let’s look at a slightly more complex calculation that could actually come in handy when you’re negotiating an investment deal. Imagine that you’re heading into an investment meeting looking to invest some cash with a promise of a \$5,000 payout 5 years in the future. How much should you be willing to pay?

The answer to this question hinges on what your alternatives are. Let’s assume you have an ongoing solid investment that returns 11 percent (lucky you!). The bottom line for the deal is whether it can do better than your 11 percent return. To figure this out, you can use a standard financial formula called present value, which looks like the following:

`PV = (FV) / (1 + rate)years`

where

```PV = Present value – how much you have to invest now
FV = Future value – how much you’ll get in the future
Rate = the interest rate you can get
Time = how much time before you collect```

What the formula spits out is how much you’d need to invest now (PV) to get some fixed amount (FV) at some time in the future by using some compound interest rate.

Going into the deal, if you’re asked to invest more than your PV, decline the deal because you can make more elsewhere. If you’re asked to invest less than PV, go for the deal. Before walking into the deal, you call your voice assistant, as follows:

```C: How much are they promising you?
U: 5 0 0 0
C: How many years?
U: 5
C: What’s your current rate of return?
U: 1 1
C: For a future value of 5,000 dollars, for 5 years, given your capability to earn at 11 percent , do not pay more than 2,967 dollars. Glad to be of service boss. Good bye.```

To get this to work, all you need is some JavaScript to code the PV formula:

```    function getPV(futureVal, years, rate) {
var base = (1 + (rate*.01));
var denom = Math.pow(base, years);

return Math.round(futureVal / denom);
}```

In the above function you declare two JavaScript variables (base and denom) and use them to do exponentiation using the Math.pow function, one of the many built-in ECMAScript library functions.

To eliminate annoying decimal values, round off using the Math.round function.

Listing 2 shows the code.

Listing 2 Voice XML future value calculator

```<?xml version="1.0" encoding="UTF-8"?>
<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2001/vxml
http://www.w3.org/TR/Voice XML20/vxml.xsd">

<script> <![CDATA[

function getPV(futureVal, years, rate) {
var base = (1 + (rate*.01));
var denom = Math.pow(base, years);

return Math.round(futureVal / denom);
}

]]> </script>

<form id="computePresentValue">
<field name="fv" type="digits">
<prompt>how much are they promising you?</prompt>
</field>

<field name="years" type="digits">
<prompt>how many years?</prompt>
</field>

<field name="rate" type="digits">
<prompt>what’s your current rate of return?</prompt>
</field>

<filled>
<prompt>
For a future value of <value expr="fv"/> dollars, for
<value expr="years"/> years,
at rate of <value expr="rate"/> percent,
Do not pay more than
<value expr="getPV(fv, years, rate) "/> dollars.

Glad to be of service boss. good bye.
</prompt>
</filled>

</form>
</vxml>```