Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 9 of 9

02222012, 12:49 AM #1
 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; 02232012 at 12:48 AM.
02222012, 01:03 AM
#2
Ummm...and do you know the FORMULA for finding the required payment?
I don't understand this loop, at all:
It's a loop in name only, as the only value of j that will be displayed is 2.Code:for ( var j = 2; j <= 2; j++ ) { document.write("<td align=right>" + j + "</td>"); }
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; 02222012 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.
02222012, 01:21 AM
#3
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 (02222012)
02222012, 01:21 AM
#4
 Join Date
 Jan 2012
 Location
 Charleston, SC
 Posts
 23
 Thanks
 9
 Thanked 0 Times in 0 Posts
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.
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.Code:var x = Math.pow(1 + interest, payments); return monthly = (principal * x * interest) / (x1);
Any suggestions would be most appreciated.
02222012, 01:26 AM
#5
 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 25.
02222012, 01:31 AM
#6
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"> </td></tr> <tr><td>36 months</td><td id="m36"> </td></tr> <tr><td>48 months</td><td id="m48"> </td></tr> <tr><td>60 months</td><td id="m60"> </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 (02222012)
02222012, 01:37 AM
#7
SO those are pages you put down on your "never look at this site again" list.I thought of this and searched for "calling a function within document.write", but after several pages telling me it wasn't possible,
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.
02222012, 01:41 AM
#8
Using your function in the loop/document.write:
EDITED: Removed a line that didn't belong.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) / (x1); // 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>
Last edited by Old Pedant; 02222012 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.
02222012, 01:45 AM
#9
 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