?
Solved

Javascript calculate compound interest

Posted on 2003-02-23
18
Medium Priority
?
3,570 Views
Last Modified: 2012-08-14
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
Comment
Question by:daniaz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 6
18 Comments
 
LVL 3

Expert Comment

by:vosk
ID: 8007145
Is something like that?
<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)
0
 
LVL 3

Accepted Solution

by:
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

by:daniaz
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:daniaz
ID: 8007518
ah could make the '2' using alt+0178
0
 
LVL 3

Expert Comment

by:vosk
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

by:vosk
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

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

Author Comment

by:daniaz
ID: 8024564
<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>&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

by:daniaz
ID: 8024716
<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>&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

by:vosk
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

by:vosk
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>
<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>&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
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.value;
r = document.forms[calc].r.value;
n = document.forms[calc].n.value;
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.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

by:vosk
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

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

Expert Comment

by:vosk
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

by:vosk
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.
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>&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

by:jaysolomon
ID: 10083404
*******************************************************
******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
0
 
LVL 3

Expert Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question