Solved

Javascript calculate compound interest

Posted on 2003-02-23
Medium Priority
4,068 Views
Hi,

i have a question.

how to create a Javascript and create a HTML documents that enable user to calculate compound interest.
TEXT component will include(user enter):
1) principal amount
2) yearly interest rate
3) number of years

formula
a=p(1+r)n

where
p is the principal(original) amount invested
r is the annual interest rate
n is the number of years(small powered n)
a is the amount on deposit at the end of the 'n'th year

there should be a button that cause the VBscript to execute and calculate the interest.
and i would like to display the result in another TEXT component. if there any TEXT component is left empty, a MsgBox will display indicating the error. this should be a function procedure calculation..

thank you ;)
0
Question by:daniaz
• 10
• 6

LVL 3

Expert Comment

ID: 8007145
Is something like that?
<html>
<script language="JavaScript">
function A()
{
var a, p, r, n;
p = document.calc.p.value;
r = document.calc.r.value;
n = document.calc.n.value;
if (p == "" || r == "" || n == "")
else
{a = 1 - - r;
a = p * a * n;
};

}
function P()
{
var a, p, r, n;
a = document.calc.a.value;
r = document.calc.r.value;
n = document.calc.n.value;
if (a == "" || r == "" || n == "")
else
{p = 1 - - r;
p = p * n;
p = a / p;
};
}

function R()
{
var a, p, r, n;
a = document.calc.a.value;
p = document.calc.p.value;
n = document.calc.n.value;
if (a == "" || p == "" || n == "")
else
{r = p * n;
r = a / r;
r = r - 1;
};
}

function N()
{
var a, p, r, n;
a = document.calc.a.value;
p = document.calc.p.value;
r = document.calc.r.value;
if (a == "" || p == "" || r == "")
else
{n = 1 - - r;
n = p * n;
n = a / n;
};
}
</script>
<body>
<form name="calc">
<center>
a <input type="text" name="a" value="" size="4"> =
p <input type="text" name="p" value="" size="4">
* ( 1 + r <input type="text" name="r" value="" size="4"> ) *
n <input type="text" name="n" value="" size="4">
</center><center>
<input type="button" name="calcb" value="CALCULATE A" onClick="A();">
<input type="button" name="calcb" value="CALCULATE P" onClick="P();">
<input type="button" name="calcb" value="CALCULATE R" onClick="R();">
<input type="button" name="calcb" value="CALCULATE N" onClick="N();">
</center>
</form>
</body>
</html>
I suppose that I don't get any error calculating the equations.
jbosch(vosk)
0

LVL 3

Accepted Solution

vosk earned 200 total points
ID: 8007172
Sorry, I've seen now...
To display the results on a textfield you have to change the alert("a = " + a) (example, do that in all functions) for something to display the result on a textfield:
document.calc.result.value = a
and add that textfield on the body and inside the form:
<input type="text" name="result" value="">

Also I forget another thing: that's only for MSIE.
jbosch(vosk)
0

Author Comment

ID: 8007448
hi

oh there quite a bit of mistake there on the formula.
suppose to be

a=p(1+r)n

where n is 'n'th power
sorry i cant type the 'n' to be like this 'Â²'.
0

Author Comment

ID: 8007518
ah could make the '2' using alt+0178
0

LVL 3

Expert Comment

ID: 8008117
Well, I have been trying but it seems difficult.
Dp you want a script to get the "n" value? Or always the "n" will be a predefined date?
jbosch(vosk)
0

LVL 3

Expert Comment

ID: 8008326
The 3rd function may be:
a = p * (1 + r) ^n

a / p = (1 + r) ^n

ln (a / p) = n * ln (1 + r)

n = [ln (a / p)] / [ln (1 + r)]
The problem is thet in JavaScript there's only defined the LN10 and LN2 (I think), so if you enter Math.LN5 it returns "undefined". Then if someone know how to calculate the ln then your question will be resolved.
jbosch(vosk)
0

Author Comment

ID: 8024297
what u mean by "3rd function maybe"?
0

Author Comment

ID: 8024564
<HTML>
<TITLE>Compound Interest</TITLE>
<SCRIPT language="JavaScript">

function calculate()
{

var a, p, r, n;

a = eval(document.calc.a.value);
p = eval(document.calc.p.value);
r = eval(document.calc.r.value);
n = eval(document.calc.n.value);

if (a == "" || p == "" || r == "" || n == "")

else
{a = p * Math.pow((1 + r ), n);

document.calc.a.value = a
};
}

</SCRIPT>
<P><FONT SIZE="5">Calculator for compound interest</FONT></P>

<P><font size="5">Formula</font></P>

<P><FONT SIZE="5">a = p(1 + r)</FONT><SUP><FONT SIZE="5">n</FONT></SUP></P>
<TR>
<TD>a</TD>
<TD>&nbsp;amount on deposit at the end of the nth year</TD>
</TR>
<TR>
<TD>p</TD>
<TD>principal (original) amount invested</TD>
</TR>
<TR>
<TD>r</TD>
<TD>annual interest rate</TD>
</TR>
<TR>
<TD>n</TD>
<TD>number of years</TD>
</TR>
</TABLE>

<P><FONT SIZE="4">Enter values for the above formula:</FONT></P>

<FORM name="calc">
<P>a <input type="text" name="a" value="" size="4"> = p <INPUT TYPE="TEXT" NAME="p" SIZE="4" VALUE="">
* (1 + r <INPUT TYPE="TEXT" NAME="r" SIZE="4" VALUE="">) <SUP><FONT SIZE="4">n</FONT></SUP>
<INPUT TYPE="TEXT" NAME="n" SIZE="4" VALUE=""></P>
<P><input type="button" name="calcb" value="Calculate" onclick="calculate()"></P>
</FORM>

</HTML>

i got problem while display Msgbox indicating error.
0

Author Comment

ID: 8024716
<HTML>
<TITLE>Compound Interest</TITLE>
<SCRIPT language="JavaScript">

function calculate()
{

var a, p, r, n;

a = eval(document.calc.a.value);
p = eval(document.calc.p.value);
r = eval(document.calc.r.value);
n = eval(document.calc.n.value);

if (a == "" || p == "" || r == "" || n == "")

else
{a = p * Math.pow((1 + r ), n);

document.calc.a.value = a
};
}

</SCRIPT>
<P><FONT SIZE="5">Calculator for compound interest</FONT></P>

<P><font size="5">Formula</font></P>

<P><FONT SIZE="5">a = p(1 + r)</FONT><SUP><FONT SIZE="5">n</FONT></SUP></P>
<TR>
<TD>a</TD>
<TD>&nbsp;amount on deposit at the end of the nth year</TD>
</TR>
<TR>
<TD>p</TD>
<TD>principal (original) amount invested</TD>
</TR>
<TR>
<TD>r</TD>
<TD>annual interest rate</TD>
</TR>
<TR>
<TD>n</TD>
<TD>number of years</TD>
</TR>
</TABLE>

<P><FONT SIZE="4">Enter values for the above formula:</FONT></P>

<FORM name="calc">
<P>a <input type="text" name="a" value="" size="4"> = p <INPUT TYPE="TEXT" NAME="p" SIZE="4" VALUE="">
* (1 + r <INPUT TYPE="TEXT" NAME="r" SIZE="4" VALUE="">) <SUP><FONT SIZE="4">n</FONT></SUP>
<INPUT TYPE="TEXT" NAME="n" SIZE="4" VALUE=""></P>
<P><input type="button" name="calcb" value="Calculate" onclick="calculate()"></P>
</FORM>

</HTML>

i got problem while display Msgbox indicating error.
0

LVL 3

Expert Comment

ID: 8026746
Sorry, I didn't entered since some days ago.
There's a problem on the place where display the result.
Wait a moment please, and I'll try to see what happens.
jbosch
0

LVL 3

Expert Comment

ID: 8026905
Finally I seen: I understood that you wanted to be able to calculate all the parameters, but the only unknown value is the "a", no?
And where is the problem? I used your code and there's no error on the alertbox:
<html>
<script language="JavaScript">
function A()
{
var a, p, r, n;
p = document.calc.p.value;
r = document.calc.r.value;
n = document.calc.n.value;
if (p == "" || r == "" || n == "")
else
{a = p * Math.pow((1 + r ), n);
};
}
</script>
<body>
<P><FONT SIZE="5">Calculator for compound interest</FONT></P>

<P><font size="5">Formula</font></P>

<P><FONT SIZE="5">a = p(1 + r)</FONT><SUP><FONT SIZE="5">n</FONT></SUP></P>
<TR>
<TD>a</TD>
<TD>&nbsp;amount on deposit at the end of the nth year</TD>
</TR>
<TR>
<TD>p</TD>
<TD>principal (original) amount invested</TD>
</TR>
<TR>
<TD>r</TD>
<TD>annual interest rate</TD>
</TR>
<TR>
<TD>n</TD>
<TD>number of years</TD>
</TR>
</TABLE>

<P><FONT SIZE="4">Enter values for the above formula:</FONT></P>

<form name="calc">

a <input type="text" name="a" value="" size="4"> =
p <input type="text" name="p" value="" size="4">
* ( 1 + r <input type="text" name="r" value="" size="4"> ) *
n <input type="text" name="n" value="" size="4">
<br>
<input type="button" name="calcb" value="CALCULATE A" onClick="A();">
</form>
</body>
</html>
I'm using MSIEv5.5, and remeber that with JavaScript you have to use
msgbox("That's the msgbox");
But if you are using NSN then you will have to change the script for the next script:
<script language="JavaScript">
function A()
{
var a, p, r, n;
p = document.forms[calc].p.value;
r = document.forms[calc].r.value;
n = document.forms[calc].n.value;
if (p == "" || r == "" || n == "")
else
{a = p * Math.pow((1 + r ), n);
};
}
</script>

and if you want to display the result on the "a" textfield add that on the script (before or after the alert):
document.forms[calc].p.value = a;

I cannot test that on my NSNv4.5 because I cannor run the browser (I don't know why), but I think that there's no error.
jbosch(vosk)
0

LVL 3

Expert Comment

ID: 8027723
Ah, and for "3rd function maybe" I mean that the 3rd function of the code that I posted the first time (the function to calculate the "n") must be able to calculate that expression (I supose that there wasn't any error).
But the problem to get the LN(number) is that I think that in JavaScript there's only defined 2 LN:
LN10 and LN2
so if you try to calculate the LN6 then it will return an error.
But if you only want to be able to get the "a" value (you need to know the "p", "r" and "n" values, so there's no problem because you won't use the LN).
Sorry if my english isn't correct, but I only writte in english here, in EE (and never speak).
jbosch(vosk)
0

Author Comment

ID: 8040593
haha the answer on the calculation all wrong. need an expert here.
0

LVL 3

Expert Comment

ID: 8051890
Now yes:
just search
a = p * Math.pow((1 + r ), n);
on your JavaScript code and change for
a = p * Math.pow((1 - - r ), n);
See that I used - - instead +
jbosch(vosk)
0

LVL 3

Expert Comment

ID: 8072038
Hello, is someone here?
There are some examples that I've calculated with my last post modification for your code:
r = 1 and n = 1 returns a = 2
p = 3, r = 5 and n = 4 returns a = 3888
p = 0.3 , r = 4.5 and n = 1.8 returns a = 6.453184...
All correct.
<html>
<script language="JavaScript">
function A()
{
var a, p, r, n;
p = document.calc.p.value;
r = document.calc.r.value;
n = document.calc.n.value;
if (p == "" || r == "" || n == "")
else
{a = p * Math.pow((1 - - r ), n);
};
}
</script>
<body>
<P><FONT SIZE="5">Calculator for compound interest</FONT></P>

<P><font size="5">Formula</font></P>

<P><FONT SIZE="5">a = p(1 + r)</FONT><SUP><FONT SIZE="5">n</FONT></SUP></P>
<TR>
<TD>a</TD>
<TD>&nbsp;amount on deposit at the end of the nth year</TD>
</TR>
<TR>
<TD>p</TD>
<TD>principal (original) amount invested</TD>
</TR>
<TR>
<TD>r</TD>
<TD>annual interest rate</TD>
</TR>
<TR>
<TD>n</TD>
<TD>number of years</TD>
</TR>
</TABLE>

<P><FONT SIZE="4">Enter values for the above formula:</FONT></P>

<form name="calc">

a <input type="text" name="a" value="" size="4"> =
p <input type="text" name="p" value="" size="4">
* ( 1 + r <input type="text" name="r" value="" size="4"> ) *
n <input type="text" name="n" value="" size="4">
<br>
<input type="button" name="calcb" value="CALCULATE A" onClick="A();">
</form>
</body>
</html>
Maybe an expert would ask you correctly at the first time, but finally that code works well.
Please, take a look and say if that is what you where searching.
jbosch(vosk)
0

LVL 16

Expert Comment

ID: 10083404
*******************************************************
*******************************************************
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area:
RECOMMENDATION:

- [Points to vosk Grade A]

*******************************************************
*******************************************************

jAy
EE Cleanup Volunteer
0

LVL 3

Expert Comment

ID: 10136609
Thank's to jaysolomon for the recmmendation and thank's to  SpazMODic for the points.
jbosch(vosk)
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'â€¦
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your projectâ€™s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQueryâ€¦
Suggested Courses
Course of the Month12 days, 17 hours left to enroll