/    Sign up×
Community /Pin to ProfileBookmark

Calculation I’ve failed to get to work!!!

I’ve failed to get this calculation to work. It is to calculate compound interest on money in a building society.

compound interest = investment(1+interest rate/100)*amount of years.

It all works fine -until the calculation is put in. I have put in some alerts to pass the values of the variables back to screen and they are all ok….

When the button is pressed it should show the amount including interest in an alert box.

Here is my code:

<html>
<head>
<title>Compound Interest Calculator</title>
<script language = “Javascript”>
var myinvestamount =0;
var myinterestrate =0;
var mymonthstoinvest =0;
var mycompoundinterest=0;

function checkform()
{
var valid = true;
myinvestamount = CustForm.Amount.value;
myinterestrate = CustForm.Rate.value;
mymonthstoinvest = CustForm.Time.value;

if (myinvestamount ==””){
alert(“No data entered in Amount to Invest Textbox”);
valid=false;
}
if (myinterestrate ==””) {
alert(“No data entered in Intrest Rate Textbox”);
valid=false;
}
if (mymonthstoinvest ==””) {
alert(“No data entered in Time to Invest Textbox”);
valid=false;
}
return valid;

}

function calculate()
{
alert(“amount ” + myinvestamount + ” rate ” + myinterestrate + ” months ” + mymonthstoinvest);
mycompoundinterest = myinvestamount((1+myinterestrate/100)*mymonthstoinvest/12);
window.alert (mycompoundinterest);
window.alert (“done calculate”);
}

function displaypopup()
{
window.alert (“done displaypopup”);
}

function doall()
{
checkform()
calculate()
window.alert (“done doall”);
return true;
}
</script>

</head>
<body>
<h3>Please Enter Your Data in the Boxes Below</h3>
<form name = “CustForm” onsubmit = “return doall()” method = “post”>
Total amount to invest(£) <input type = “text” size = 50 name = “Amount”><p>
Interest Rate <input type = “text” size = 10 name = “Rate”><p>
Number of Months to Invest <input type = “text” size = 10 name = “Time”><p>
<input type = submit value = “Submit”><p>
</form>
</body>
</html>

Hope someone can help……I’m desperate with this!

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoNov 09.2005 — value returns strings. You have to use on of the following to make it numbers:

parseInt() . . . whole numbers only

parseFloat() . . . other numbers

eval() . . . anything
Copy linkTweet thisAlerts:
@TheBearMayNov 09.2005 — You forgot a "*" should be:

mycompoundinterest = myinvestamount[b][color=red]*[/color][/b]((1+myinterestrate/100)*mymonthstoinvest/12);
Copy linkTweet thisAlerts:
@madman070578authorNov 09.2005 — Thanks guys....between both of you I've sorted it!

Perhaps you can help me with two more things - I need to round it up to 2 decimal places - is that easy?

also I need to have the result displayed in a popup window - can anyone give me the code to put in the function to get a popup window and write the result to the screen in it?
Copy linkTweet thisAlerts:
@konithomimoNov 09.2005 — 
  • 1. mycompoundinterst = ((math.Round(mycompoundinterest * 100))/100).toFixed(2);


  • 2. Here is an example:
    <i>
    </i>&lt;script type="text/javascript&gt;
    {
    function info(p) {
    var msg=window.open("","smallwin","height=340,width=250,left=10,top=40");
    msg.document.write('&lt;html&gt;&lt;title&gt;Windows!&lt;/title&gt;');
    msg.document.write('&lt;body onblur=window.close()&gt;');
    msg.document.write('Search Instructions:');
    msg.document.write('&lt;br&gt;1.' + p);
    msg.document.write('&lt;/body&gt;&lt;/html&gt;');

    }
    &lt;/script&gt;
  • Copy linkTweet thisAlerts:
    @madman070578authorNov 09.2005 — Great stuff guys - got the popup working. I've got it diplaying the amount, then the amount with compound interest - and I was going to calculate the interest then by subtracting the amount with compound interest away from the amount - but it says NaN rather than the amount! I know it means not a number but I don't know which way to correct it..... hope someone can give me some guidance.

    Also since adding the code to reduce the answer to two decimal places it doesn't work at all - perhaps someone can point out my error....

    this is the code in question....

    mycompoundinterest = myinvestamount*((1+myinterestrate/100)*mymonthstoinvest/12); <----this line works fine

    mycompoundinterest = ((math.Round(mycompoundinterest * 100))/100).toFixed(2); <---- when I add this it doesn't work at all

    This is my complete code at the moment......

    <html>

    <head>

    <title>Compound Interest Calculator</title>

    <script language = "Javascript">

    var myinvestamount =0;

    var myinterestrate =0;

    var mymonthstoinvest =0;

    var mycompoundinterest=0;

    mycompoundinterest=parseFloat(mycompoundinterest);

    function checkform()

    {

    var valid = true;

    myinvestamount = CustForm.Amount.value;

    myinterestrate = CustForm.Rate.value;

    mymonthstoinvest = CustForm.Time.value;

    if (myinvestamount ==""){
    alert("No data entered in Amount to Invest Textbox");
    valid=false;
    }
    if (myinterestrate =="") {
    alert("No data entered in Intrest Rate Textbox");
    valid=false;
    }
    if (mymonthstoinvest =="") {
    alert("No data entered in Time to Invest Textbox");
    valid=false;
    }
    if (isNaN(myinvestamount)){
    alert("Amount is not a number!");
    valid=false;
    }
    if (isNaN(myinterestrate)){
    alert("Amount is not a number!");
    valid=false;
    }
    if (isNaN(mymonthstoinvest)){
    alert("Amount is not a number!");
    valid=false;
    }
    return valid;

    }

    function calculate()

    {

    alert("amount " + myinvestamount + " rate " + myinterestrate + " months " + mymonthstoinvest);

    mycompoundinterest = myinvestamount*((1+myinterestrate/100)*mymonthstoinvest/12);

    mycompoundinterest = ((math.Round(mycompoundinterest * 100))/100).toFixed(2);

    window.alert (mycompoundinterest);

    window.alert ("done calculate");

    }

    function displaypopup()

    {

    {

    var msg=window.open("","smallwin","height=340,width=250,left=10,top=40");

    msg.document.write('<html><title>Results</title>');

    msg.document.write('<body onblur=window.close()>');

    msg.document.write(" £" +myinvestamount);

    msg.document.write(" £" +mycompoundinterest);

    msg.document.write(" £" +mycompoundinterest-myinvestamount);

    msg.document.write('<br>1.' + p);

    msg.document.write('</body></html>');

    }

    window.alert ("done displaypopup");

    }

    function doall()

    {

    checkform()

    calculate()

    displaypopup()

    window.alert ("done doall");

    return true;

    }

    </script>

    </head>

    <body>

    <h3>Please Enter Your Data in the Boxes Below</h3>

    <form name = "CustForm" onsubmit = "return doall()" method = "post">

    Total amount to invest(£) <input type = "text" size = 50 name = "Amount"><p>

    Interest Rate <input type = "text" size = 10 name = "Rate"><p>

    Number of Months to Invest <input type = "text" size = 10 name = "Time"><p>

    <input type = submit value = "Submit"><p>

    </form>

    </body>

    </html>
    Copy linkTweet thisAlerts:
    @TheBearMayNov 10.2005 — Should be:

    mycompoundinterest = (Math.round((mycompoundinterest * 100)/100)).toFixed(2);

    Javascript is case sensitive.

    What is "p" in this line:

    msg.document.write('<br>1.' + p);

    Doesn't seem to be defined anywhere.
    Copy linkTweet thisAlerts:
    @TheBearMayNov 10.2005 — This should fix your NaN. Since you were doing string concatenation it converted the first number to text before trying to subtract. Still need to resolve what you're setting "p" to though

    function displaypopup()
    {
    {
    var msg=window.open("","smallwin","height=340,width=250,left=10,top=40");
    msg.document.write('&lt;html&gt;&lt;title&gt;Results&lt;/title&gt;');
    msg.document.write('&lt;body onblur=window.close()&gt;');
    msg.document.write(" £" +myinvestamount + "&lt;br /&gt;");
    msg.document.write(" £" +mycompoundinterest + "&lt;br /&gt;");
    msg.document.write(" £" +(mycompoundinterest-myinvestamount) + "&lt;br /&gt;");
    msg.document.write('&lt;br&gt;1.' + p);
    msg.document.write('&lt;/body&gt;&lt;/html&gt;');

    }
    Copy linkTweet thisAlerts:
    @madman070578authorNov 10.2005 — The p variable wasn't needed - that was just a cut and paste of some code konithomimo suggested further up the thread of an example for a popup window.

    Thanks to both you thebearmay and konithomimo - you have both helped loads. Sorry for all the hassle - sure you can see I'm a real newbie to Javascript.

    I'm now trying to improve it so the results are put in a cookie so I can display them an another page rather than a popup. Can you store more than one variable in a cookie?

    I've got it a cookie storing one variable - just need it to store two more.....

    Here is my code:

    function putCookie()

    {

    if(document.cookie != document.cookie)

    {index = document.cookie.indexOf(cookie_name);}

    else

    { index = -1;}

    if (index == -1)

    {

    document.cookie=cookie_name+"="+myinvestamount+"; expires=Monday, 04-Apr-2010 05:00:00 GMT";

    }

    }

    Really I need to add the variables myinterestrate, mymonthstoinvest, and mycompoundinterest......
    ×

    Success!

    Help @madman070578 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...