943,772 Members | Top Members by Rank

Ad:
Oct 27th, 2008
0

for loop help needed

Expand Post »
I am trying to write a basic amortization calculator program in JavaScript and I am at a standstill. I have researched my issue online for hours without being able to come up with a resolution. In my program I have created a "for" loop which loops for the appropriate loan term, however, none of my other calculations change in the loop. They all remain identical to the first row (except for the number of months). Below is the code that I have thus far. I am not looking for anyone to provide the actual correct code, but simply ideas regarding why my loop does not work properly or a direction to head into. I have found tons of slightly different javascript codes regarding amortizations schedules, but I don't want to use those and I would like to learn how to get my own code to work. Any help would be appreciated.


function amort(balance, interestRate, numMonths)
{
	var monthlyRate = interestRate / 12;
	var payment = balance * (monthlyRate / (1 - Math.pow(1 + monthlyRate, -terms)));
	
	var result = "Loan amount: $" + balance.toFixed(2) + "<br />" + 
	"Interest rate: " + (interestRate * 100).toFixed(2) + "%<br />" +
	"Number of months: " + terms + "<br />" +
	"Monthly payment: $" + payment.toFixed(2) + "<br />" +
	"Total paid: $" + (payment * terms).toFixed(2) + "<br /><br />";
	
	result += "<table border='1'><tr><th>Month</th><th>Balance</th>" + //creates table headers
			"<th>Interest</th><th>Principal</th>";

			
	var interestPaid = balance * monthlyRate; //calculates the interest paid each month
	var principalPaid = payment - interestPaid; //calculates the principal paid each month
	var newBalance = balance - principalPaid; // calculates a new monthly balance 
						
		for (var i = 1; i <= numMonths; i++ ){  //starts loop and increments number of months by one up to the inputted loan term
		result += "<tr><td>" + i + "</td><td>" + balance.toFixed(2) + "</td><td>" + interestPaid.toFixed(2) + "</td>" + "<td>" + principalPaid.toFixed(2) + "</td></tr>"
				
									
				}
				
				
		result += "</table>"; 
		return result
			
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
jobojo is offline Offline
28 posts
since Oct 2008
Oct 28th, 2008
0

Re: for loop help needed

After quite a bit more trial and error and rewriting the code all over again a few times I was finally able to get my code to work properly. The problem seems to have been in my variables and math and reworking it from scratch seemed to give me a better perspective on the problem(s).
Reputation Points: 10
Solved Threads: 0
Light Poster
jobojo is offline Offline
28 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Show Nav image on mouse over text link
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Javascript opener with flash target blank





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC