Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Help needed with nested inner for loop

1. ## 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>");
}```

• 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?

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.

• 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>```

• ## Users who have thanked Old Pedant for this post:

BBoyd (02-22-2012)

• Originally Posted by Old Pedant
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.

• ## 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.

• This is probably a better way to do it, avoiding the docuemnt.write and making it interactive:
Code:
```<html>
<script type="text/javascript">
function calc(form)
{
var principal = parseFloat(form.amt.value);
if( isNaN(principal) || principal <= 0 )
{
principal = 0;
}
var apr= parseFloat(form.APR.value);
if ( isNaN(apr) || apr <= 0 || apr > 30 )
{
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>
<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>```

• ## Users who have thanked Old Pedant for this post:

BBoyd (02-22-2012)

• 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.

• 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.

• 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
•