I am not a computer programmer but have an assignment that involves javascript. I need a program that will display the interest in a savings account if I deposit $1000 at 5% interest for 10 years. I need it to show the interest each year for each of the 10 years. I can do it with multiplication of 1 through 9 but I can't figure out how to do it with this.
I was given the following info:
a=p(l+r)"n
p is principal
r is annual interest
n is number of years
a is amount on deposit at end of nth year.

Since javascript doesnt recognize exponents I was also given this.

amount = principal * Math.pow(1.0 rate, year);

also use decimals

Math.round(amount * 100)/100

Here is the program that will do multiplication facts for 1 though 9

<HTML>
<head>
<title> Tues Class example 4</title>

<script language="javascript">

function show()
{
var result;

for (var i=1; i<=9; i++)
{
for (var j=1; j<=9; j++)
{
result = j*i;
document.write(i + " x " + j + " = " + result);
}
document.write("<br>");
}
}

</script>

</head>

<body>

<p><input type="button" value="click to see the result" onClick="show()">

</form>

</body>

</HTML>

<script type="text/javascript"><!--

function a(p,r,n)
{
return (parseFloat(p) * Math.pow( (1+parseFloat(r)), parseInt(n,10) )).toFixed(2);
}

//call it as follows:
var i=1;
while(i<=10)
{
document.write( i + ". "+ a("1000.00",".05",i) +"<br />");
++i;
}

//--></script>

I do have one question about the above info...

I have been taught to create a button so that onClick it displays the calcuations. Do I need to use "for" in my code to make that possible?

As I stated I am very new to this and not a programmer. So I feel very overwhelmed by all of this.
THis is what I have used in the past...

function show() (which I don't know what this means or is telling the computer to do)
<p><input type="button" value="click ot see the result" onClick="show()">

I tried to add this but its not working either... any ideas?

Javascript has built-in functions, meaning it already comes with some functions that you can just use. It also lets you create your own functions.
show() is not a built in function.

For the following to work:
<input type="button" value="click to see the result" onClick="show()">

you would need to create a function called show. Within the show function you need to call the while loop I posted previously. The only thing however is that before the while loop you would need to write:
document.open();
and after the while's closing brace you would need:
document.close();

Try it, and let me know what happens.

Below is a complete example of how I would put the a() function that I gave you to use.

You can call show with the number of years as well as the id of the element where you want the results to appear. Example:

<input type="button" onclick="show(5,'myResults');" />


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hielo's Calculations</title>
<script type="text/javascript"><!--

function a(p,r,n)
{
return (parseFloat(p) * Math.pow( (1+parseFloat(r)), parseInt(n,10) )).toFixed(2);
}

function show(years,target)
{
    var i=1;//starting year
    var limit=10;//the default number of years if none specified
    var destination=null;//where to write the  results
    var buffer=[];//to save the partial results

    //check if user specified number of years and if so, override default
    if(parseInt(years,10))
        limit=parseInt(years,10);
    //check if user specified target element to write results and if so, override default
    if("string"==typeof(target))
    {
        destination=document.getElementById(target);
    }
    else//otherwise create a div dinaymically and append it to <body>. The results will be withing <div id='results'></div>
    {

        if( destination=document.createElement("div") )
        {
            destination.id="target";
            document.getElementsByTagName('body')[0].appendChild(destination);
        }
    }

    while(i<=limit)
    {
        buffer[buffer.length] = ( i + ". " + a("1000.00",".05",i) );
        ++i;
    }

    if(destination)
        destination.innerHTML = "<div>"+buffer.join("</div><div>")+"</div>";
    else//this execute only in very old browsers.
        alert(buffer.join("\n"));
}
//--></script>
</head>
<body>
<form>
<input type="button" value="Compute" onclick="show(10,'myResults');"/>
</form>
<div id="myResults"></div>
</body>
</html>

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.