0

Here is the problem:

Write a program to find future value of monthly investments. Start with an initial investment, make a deposit every 30days, calculate interest on principle compounded daily, display a table showing beginning balance, deposit for the year, interest
earned for the year, and ending balance. The table should display the aforementioned
information for a duration of your present age to 65. Display result in a table format as shown below.

Here is the code so far.....

    <html>
    <head>
    <title>Future Value Calculation</title>
    <style type = "text/css">
    table { width: 100% }
    th    { text-align : left } 
    th span { color: #008; margin-left: 15px; }
    </style>
    <script type = "text/javascript">
    window.onload = function()
    {
    var EndBal;
    var principal = 2000.00;
    var rate = 0.12;
    var deposit = 1200;
    var interest;
    var BegBal = principal + deposit;
    for ( var age = 16; age >= 1; --age )
    {
    // Do calculations
    EndBal = ((principal + deposit) * Math.pow(1 + rate, age ));
    interest = ((principal + deposit) * Math.pow(1 + rate, age ))-(principal+deposit);
    // Insert row
    var row = getById("tBody").insertRow(0); //Insert row at index 0
    // Create cells for the created row
    // Empty cells are set to   to be displayed in the table
    row.insertCell(0).innerHTML = age+50; 
    row.insertCell(1).innerHTML = EndBal.toFixed(2);
    row.insertCell(2).innerHTML = interest.toFixed(2);
    row.insertCell(3).innerHTML = deposit;
    row.insertCell(4).innerHTML = BegBal.toFixed(2);
    BegBal = EndBal;
    }
    // Set the header values
    getById("spnInitial").innerHTML = principal.toFixed(2);
    getById("spnRate").innerHTML = rate.toFixed(2);
    getById("spnDeposit").innerHTML = deposit;
    getById("spnInvestment").innerHTML = EndBal.toFixed(2);
    }
    // Helper function to get element by id
    function getById(id)
    {
    return document.getElementById(id);
    }
    </script>
    </head>
    <body>
    <table border="1">
    <thead>
    <tr><th colspan="5">Future Value Calculation</th></tr>
    <tr><th colspan="5">Initial Investment:<span id="spnInitial"></span></th></tr>
    <tr><th colspan="5">Interest Rate:<span id="spnRate"></span></th></tr>
    <tr><th colspan="5">Deposit every 30 days:<span id="spnDeposit"></span></th></tr>
    <tr><th colspan="5">Investment started:<span id="spnInvestment"></span></th></tr>
    <tr>
    <th>Age</th>
    <th>Beg Bal</th>
    <th>Interest</th>
    <th>Deposits</th>
    <th>Ending Bal</th>
    </tr>
    </thead>
    <tbody id="tBody">
    </tbody>
    </table>
    </body>
    </html>

But it does not calculate correctly. I need someone to help out with the loop...

2
Contributors
14
Replies
53
Views
3 Years
Discussion Span
Last Post by AleMonteiro
0

I don't think this is correct at all, but it makes much more sense than yours do... maybe it can help:

    <html>
    <head>
    <title>Future Value Calculation</title>
    <style type = "text/css">
    table { width: 100% }
    th    { text-align : left } 
    th span { color: #008; margin-left: 15px; }
    </style>
    <script type = "text/javascript">
    window.onload = function()
    {

        var EndBal;
        var principal = 2000.00;
        var rate = 0.12;
        var deposit = 1200;
        var interest;
        var BegBal = principal;
        var initialAge = 16;
        for ( var age = initialAge, i=1; age <= 65; age++, i++ )
        {   
            EndBal = ((principal + (deposit*12* i)) * Math.pow(1 + rate, i ));
            interest = EndBal-(principal+(deposit*12*i));
            // Insert row
            var row = getById("tBody").insertRow(getById("tBody").rows.length); //Insert row at index 0
            // Create cells for the created row
            // Empty cells are set to   to be displayed in the table
            row.insertCell(0).innerHTML = age; 
            row.insertCell(1).innerHTML = formatNum(BegBal);
            row.insertCell(2).innerHTML = formatNum(interest);
            row.insertCell(3).innerHTML = formatNum((deposit*12*i));
            row.insertCell(4).innerHTML = formatNum(EndBal);
            BegBal = EndBal;
        }
        // Set the header values
        getById("spnInitial").innerHTML = formatNum(principal);
        getById("spnRate").innerHTML = rate.toFixed(2);
        getById("spnDeposit").innerHTML = formatNum(deposit);
        getById("spnInvestment").innerHTML = formatNum(EndBal);
    }

    // Format number
    function formatNum(val)
    {
        return val.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
    }
    // Helper function to get element by id
    function getById(id)
    {
        return document.getElementById(id);
    }
    </script>
    </head>
    <body>
    <table border="1">
    <thead>
    <tr><th colspan="5">Future Value Calculation</th></tr>
    <tr><th colspan="5">Initial Investment:<span id="spnInitial"></span></th></tr>
    <tr><th colspan="5">Interest Rate:<span id="spnRate"></span></th></tr>
    <tr><th colspan="5">Deposit every 30 days:<span id="spnDeposit"></span></th></tr>
    <tr><th colspan="5">Investment started:<span id="spnInvestment"></span></th></tr>
    <tr>
    <th>Age</th>
    <th>Beg Bal</th>
    <th>Interest</th>
    <th>Deposits</th>
    <th>Ending Bal</th>
    </tr>
    </thead>
    <tbody id="tBody">
    </tbody>
    </table>
    </body>
    </html>
0

no, it did not yield the correct values...

here is the problem again,

"Write a program to find future value of monthly investments. Start with an initial investment, make a deposit every 30days, calculate interest on principle compounded daily, display a table showing beginning balance, deposit for the year, interest
earned for the year, and ending balance. The table should display the aforementioned
information for a duration of your present age to 65. Display result in a table format as shown below."

Future Value Calculation
Initial Investment: $2000
Interest Rate: 12%
Deposit every 30 days: $100
Investment started: 50
Age Beg Bal Interest    Deposits    Ending Bal
51  2000    324.65  1200    3524.65 
52  3524.65     519.01  1200    5243.66 
53  5243.66     738.14  1200    7181.79 
54  7181.79     985.2   1200    9366.99 
55  9366.99     1263.76     1200    11830.74 
56  11830.74    1577.82     1300    14708.57 
57  14708.57    1944.67     1200    17853.24 
58  17853.24    2345.54     1200    21398.77 
59  21398.77    2797.5  1200    25396.28 
60  25396.28    3307.08     1200    29903.36 
61  29903.36    3881.62     1200    34984.98 
62  34984.98    4529.4  1300    40814.38 
63  40814.38    5272.5  1200    47286.88 
64  47286.88    6097.58     1200    54584.46 
65  54584.46    7027.83     1200    62812.29 
0

This table is also wrong... if you make 1200 deposit every 30 days, how come in one year you only deposited 1200? This make no sense.

0

because there is supposed to be an "additional" deposit. And there in lies my need for help...

0

So the label should be "Deposit Per Month", or something like that.

My point is, your problem, at the moment, seems more about the logic and mathematics about investments than with coding itself.

0

"MY" point precisely... THIS is what I need help in. I suppose i need something else to arrive at the "extra" deposit contirbution. But I don't know how to do that. The rest is ok I think.

0

instead of calculating interest on a monthly or yearly basis, this is asking to do it on a daily basis. I know, it is a bit difficult. I thought so too.

0

So... unless you know a cleaver way, I thin you'll have to loop day by day to calculate the interest.

There's probably a easier math solution, but I don't know it.

0

well, I am relentless and when I find the solution I will share it with you.. :)

0

OK I think this works.....

<script language="JavaScript">

var investment     = 2000;
var interest_rate  = .12;
var deposit_amount = 100;
var start_age      = 40;
var end_age        = 67;
var beg_balance    = 0;
var end_balance    = 0;
var daily_interest_rate = interest_rate / 365;
var calculated_interest = 0;
var accrued_interest    = 0;
var cumulative_deposits = 0;
var days           = 0;

document.write("<table>")
document.write("<tr><th>Age</th><th>Beg Bal</th><th>Interest</th><th>Deposits</th><th>Ending Bal</th></tr>")
beg_balance = investment;

for (var yearly = start_age + 1; yearly <= end_age; yearly++) 
    {
        for (var daily = 1; daily <= 365; daily++) 
        {
            days++;
            calculated_interest = (daily_interest_rate) * (beg_balance + cumulative_deposits + accrued_interest);
            accrued_interest += calculated_interest;

            if (days == 30) 
            {
            days = 0;
            cumulative_deposits += deposit_amount;
            }     
        }

    end_balance = beg_balance + cumulative_deposits + accrued_interest;

    document.write("<tr>");
    document.write("<td>" + yearly.toFixed(2) + "</td><td>" + beg_balance.toFixed(2) + "</td><td>" + accrued_interest.toFixed(2) + "</td><td>" + cumulative_deposits.toFixed(2) + "</td><td>" + end_balance.toFixed(2) + "</td>");
    document.write("</tr>");

    beg_balance = end_balance;
    cumulative_deposits = 0;
    accrued_interest = 0;       
}  

document.write("</table>")
</script>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.