Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Resolved Help needed with nested inner for loop

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 02-22-2012, 01:49 AM PM User | #1 BBoyd New Coder   Join Date: Jan 2012 Location: Charleston, SC Posts: 21 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("
" + i + "" + j + "
"); document.write(""); for ( var j = 2; j <= 2; j++ ) { document.write(""); } document.write(""); }``` Last edited by BBoyd; 02-23-2012 at 01:48 AM..
 02-22-2012, 02:03 AM PM User | #2 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,938 Thanks: 75 Thanked 4,305 Times in 4,272 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("
" + j + "
"); }``` 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. __________________ 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. Last edited by Old Pedant; 02-22-2012 at 02:05 AM..
 02-22-2012, 02:21 AM PM User | #3 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,938 Thanks: 75 Thanked 4,305 Times in 4,272 Posts Example code. Though I wouldn't recommend using document.write for this. Code: ``` Loan amount: \$8700.00
APR: 6.75%
Length of loanMonthly payment
``` __________________ 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)
02-22-2012, 02:21 AM   PM User | #4
BBoyd
New Coder

Join Date: Jan 2012
Location: Charleston, SC
Posts: 21
Thanks: 9
Thanked 0 Times in 0 Posts
Quote:
 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.

 02-22-2012, 02:26 AM PM User | #5 BBoyd New Coder   Join Date: Jan 2012 Location: Charleston, SC Posts: 21 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.
 02-22-2012, 02:31 AM PM User | #6 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,938 Thanks: 75 Thanked 4,305 Times in 4,272 Posts This is probably a better way to do it, avoiding the docuemnt.write and making it interactive: Code: ```
Loan amount:
APR:
Length of loanMonthly payment
24 months
36 months
48 months
60 months
``` __________________ 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)
02-22-2012, 02:37 AM   PM User | #7
Old Pedant
Supreme Master coder!

Join Date: Feb 2009
Posts: 24,938
Thanks: 75
Thanked 4,305 Times in 4,272 Posts
Quote:
 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.

 02-22-2012, 02:41 AM PM User | #8 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,938 Thanks: 75 Thanked 4,305 Times in 4,272 Posts Using your function in the loop/document.write: Code: ``` Loan amount: \$8700.00
APR: 6.75%
Length of loanMonthly payment
``` EDITED: Removed a line that didn't belong. __________________ 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. Last edited by Old Pedant; 02-22-2012 at 03:11 AM..
 02-22-2012, 02:45 AM PM User | #9 BBoyd New Coder   Join Date: Jan 2012 Location: Charleston, SC Posts: 21 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

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 07:46 AM.