daniaz
asked on
Javascript calculate compound interest
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 ;)
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 ;)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 '²'.
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 '²'.
ASKER
ah could make the '2' using alt+0178
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)
Dp you want a script to get the "n" value? Or always the "n" will be a predefined date?
jbosch(vosk)
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)
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)
ASKER
what u mean by "3rd function maybe"?
ASKER
<HTML>
<HEAD>
<TITLE>Compound Interest</TITLE>
</HEAD>
<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 == "")
{alert("enter all numbers")};
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 >
<TABLE BORDER="1" CELLPADDING="4">
<TR>
<TD>a</TD>
<TD> 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.
<HEAD>
<TITLE>Compound Interest</TITLE>
</HEAD>
<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 == "")
{alert("enter all numbers")};
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
<TABLE BORDER="1" CELLPADDING="4">
<TR>
<TD>a</TD>
<TD> 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.
ASKER
<HTML>
<HEAD>
<TITLE>Compound Interest</TITLE>
</HEAD>
<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 == "")
{alert("enter all numbers")};
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 >
<TABLE BORDER="1" CELLPADDING="4">
<TR>
<TD>a</TD>
<TD> 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.
<HEAD>
<TITLE>Compound Interest</TITLE>
</HEAD>
<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 == "")
{alert("enter all numbers")};
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
<TABLE BORDER="1" CELLPADDING="4">
<TR>
<TD>a</TD>
<TD> 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.
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
There's a problem on the place where display the result.
Wait a moment please, and I'll try to see what happens.
jbosch
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>
<head>
<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 == "")
{alert("enter all numbers")};
else
{a = p * Math.pow((1 + r ), n);
alert("a = " + a)
};
}
</script>
</head>
<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 >
<TABLE BORDER="1" CELLPADDING="4">
<TR>
<TD>a</TD>
<TD> 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
alert("That's the alert");
instead
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.val ue;
r = document.forms[calc].r.val ue;
n = document.forms[calc].n.val ue;
if (p == "" || r == "" || n == "")
{alert("enter all numbers")};
else
{a = p * Math.pow((1 + r ), n);
alert("a = " + a)
};
}
</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.val ue = 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)
And where is the problem? I used your code and there's no error on the alertbox:
<html>
<head>
<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 == "")
{alert("enter all numbers")};
else
{a = p * Math.pow((1 + r ), n);
alert("a = " + a)
};
}
</script>
</head>
<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
<TABLE BORDER="1" CELLPADDING="4">
<TR>
<TD>a</TD>
<TD> 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
alert("That's the alert");
instead
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.val
r = document.forms[calc].r.val
n = document.forms[calc].n.val
if (p == "" || r == "" || n == "")
{alert("enter all numbers")};
else
{a = p * Math.pow((1 + r ), n);
alert("a = " + a)
};
}
</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.val
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)
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)
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)
ASKER
haha the answer on the calculation all wrong. need an expert here.
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)
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)
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.
Your entire code is:
<html>
<head>
<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 == "")
{alert("enter all numbers")};
else
{a = p * Math.pow((1 - - r ), n);
alert("a = " + a)
};
}
</script>
</head>
<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 >
<TABLE BORDER="1" CELLPADDING="4">
<TR>
<TD>a</TD>
<TD> 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)
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.
Your entire code is:
<html>
<head>
<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 == "")
{alert("enter all numbers")};
else
{a = p * Math.pow((1 - - r ), n);
alert("a = " + a)
};
}
</script>
</head>
<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
<TABLE BORDER="1" CELLPADDING="4">
<TR>
<TD>a</TD>
<TD> 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)
************************** ********** ********** *********
******PLEASE DO NOT ACCEPT THIS COMMENT AS ANSWER******
************************** ********** ********** *********
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]
Please leave any comments here within the next seven days.
************************** ********** ********** *********
******PLEASE DO NOT ACCEPT THIS COMMENT AS ANSWER******
************************** ********** ********** *********
jAy
EE Cleanup Volunteer
******PLEASE DO NOT ACCEPT THIS COMMENT AS ANSWER******
**************************
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]
Please leave any comments here within the next seven days.
**************************
******PLEASE DO NOT ACCEPT THIS COMMENT AS ANSWER******
**************************
jAy
EE Cleanup Volunteer
Thank's to jaysolomon for the recmmendation and thank's to SpazMODic for the points.
jbosch(vosk)
jbosch(vosk)
<html>
<head>
<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 == "")
{alert("enter all numbers")};
else
{a = 1 - - r;
a = p * a * n;
alert("a = " + a)
};
}
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 == "")
{alert("enter all numbers")};
else
{p = 1 - - r;
p = p * n;
p = a / p;
alert("p = " + 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 == "")
{alert("enter all numbers")};
else
{r = p * n;
r = a / r;
r = r - 1;
alert("r = " + r)
};
}
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 == "")
{alert("enter all numbers")};
else
{n = 1 - - r;
n = p * n;
n = a / n;
alert("n = " + n)
};
}
</script>
</head>
<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)