Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jan 2012
    Location
    Charleston, SC
    Posts
    23
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Help needed with nested inner for loop

    After struggling with this for the past 2 days, I've decided it is time to seek out some help.

    The program is a calculator to show the monthly payments required depending on if the terms are for 2, 3, 4, or 5 year financing. I have the following 4 variables that I need to be placed in the inner loop, but not sure how to do it.

    pay24, pay36, pay48, pay60

    The following loop creates a 4 row x 2 column table which I need it to do, but the inner loop needs to show the value of the 4 variables above.

    Can someone assist me in this problem?

    Code:
    for ( var i=24; i<=60; i+=12 ) {
    document.write("<tr>");
    document.write("<td align=center>" + i + "</td>");
      
        for ( var j = 2; j <= 2; j++ ) {
            document.write("<td align=right>" + j + "</td>");
        }
        document.write("</tr>");
    }
    Last edited by BBoyd; 02-23-2012 at 12:48 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Ummm...and do you know the FORMULA for finding the required payment?

    I don't understand this loop, at all:
    Code:
        for ( var j = 2; j <= 2; j++ ) {
            document.write("<td align=right>" + j + "</td>");
        }
    It's a loop in name only, as the only value of j that will be displayed is 2.

    What's the point of that?

    Wait a a minute: Are you saying you already have calculated the payment amounts and put them into those variables?

    That's your big goof.

    You should have put them in an array.

    Or, even simpler, you should just calculate them on the fly as part of the outer loop here.
    Last edited by Old Pedant; 02-22-2012 at 01:05 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Example code. Though I wouldn't recommend using document.write for this.
    Code:
    <html>
    <body>
    Loan amount: $8700.00<br/>
    APR: 6.75%<br/>
    <table border="1">
    <tr><th>Length of loan</th><th>Monthly payment</th></tr>
    <script type="text/javascript">
    var principal = 8700.00;
    var apr = 6.75
    
    var interest = apr / 1200;
    
    for ( var months = 24; months <= 60; months += 12 )
    {
        var payment = principal / ( ( 1 - Math.pow( 1 + interest, -months ) ) / interest );
        document.write('<tr><td>' + months + ' months</td><td>$' + payment.toFixed(2) + '</td></tr>\n');
    }
    </script>
    </table>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    BBoyd (02-22-2012)

  • #4
    New Coder
    Join Date
    Jan 2012
    Location
    Charleston, SC
    Posts
    23
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    You should have put them in an array.

    Or, even simpler, you should just calculate them on the fly as part of the outer loop here.
    We have not reached the subject of arrays yet. I have knowledge of arrays, but far from really understanding right now.

    This is the function that calculates the monthly payment.

    Code:
        var x = Math.pow(1 + interest, payments);
        return monthly = (principal * x * interest) / (x-1);
    As for calculating on the fly in the loop would be awesome, but I do not even know where to begin. However, I thought of this and searched for "calling a function within document.write", but after several pages telling me it wasn't possible, I gave up on that thought. Maybe I misunderstood what I was reading.

    Any suggestions would be most appreciated.

  • #5
    New Coder
    Join Date
    Jan 2012
    Location
    Charleston, SC
    Posts
    23
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Instructions

    Some additional information that might be useful to know. The assignment has to use a function to calculated and return the monthly payment. The loop is used to display the payment amount for years 2-5.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    This is probably a better way to do it, avoiding the docuemnt.write and making it interactive:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function calc(form)
    {
        var principal = parseFloat(form.amt.value);
        if( isNaN(principal) || principal <= 0 )
        {
            alert("invalid loan amount");
            principal = 0;
        }
        var apr= parseFloat(form.APR.value);
        if ( isNaN(apr) || apr <= 0 || apr > 30 )
        {
            alert("invalid APR amount");
            principal = 0; apr = 1;
        }
        var interest = apr / 1200;
    
        for ( var months = 24; months <= 60; months += 12 )
        {
            var payment = principal / ( ( 1 - Math.pow( 1 + interest, -months ) ) / interest );
            document.getElementById("m"+months).innerHTML = "$" + payment.toFixed(2);
        }
    }
    </script>
    </head>
    <body>
    <form>
    Loan amount: <input name="amt"/><br/>
    APR: <input name="APR" /><br/>
    <input type="button" value="Calculate payments" onclick="calc(this.form);"/>
    </form>
    <table border="1">
    <tr><th>Length of loan</th><th>Monthly payment</th></tr>
    <tr><td>24 months</td><td id="m24">&nbsp;</td></tr>
    <tr><td>36 months</td><td id="m36">&nbsp;</td></tr>
    <tr><td>48 months</td><td id="m48">&nbsp;</td></tr>
    <tr><td>60 months</td><td id="m60">&nbsp;</td></tr>
    </table>
    
    </table>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    BBoyd (02-22-2012)

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    I thought of this and searched for "calling a function within document.write", but after several pages telling me it wasn't possible,
    SO those are pages you put down on your "never look at this site again" list.

    OF COURSE you can call functions in the course of doing document.write.

    p.s.: My function and your for calculating payment are identical, except mine can be written on one line so I don't bother making it into a separate function. You can use simple algebra to prove that statement if you wish.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Using your function in the loop/document.write:
    Code:
    <html>
    <body>
    Loan amount: $8700.00<br/>
    APR: 6.75%<br/>
    <table border="1">
    <tr><th>Length of loan</th><th>Monthly payment</th></tr>
    <script type="text/javascript">
    function getPayment( principal, interest, payments )
    {
        var x = Math.pow(1 + interest, payments);
        return (principal * x * interest) / (x-1); // no reason to assign to "monthly"!!!
    }
    
    var principal = 8700.00;
    var apr = 6.75
    
    var interest = apr / 1200;
    
    for ( var months = 24; months <= 60; months += 12 )
    {
        document.write('<tr><td>' + months + ' months</td>');
        document.write('<td>$' + getPayment( principal, interest, months ).toFixed(2) + '</td></tr>\n');
    }
    </script>
    </table>
    </body>
    </html>
    EDITED: Removed a line that didn't belong.
    Last edited by Old Pedant; 02-22-2012 at 02:11 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New Coder
    Join Date
    Jan 2012
    Location
    Charleston, SC
    Posts
    23
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thank you for all your help and I will be able to use a lot of the knowledge you have shared with me. Not only for this assignment, but other assignments as well.

    Brian


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •