| | |
for loop help needed
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
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
- cannot start up XP - stuck in restart loop, keyboard frozen (Windows NT / 2000 / XP)
- Help needed with VB Assignment (Visual Basic 4 / 5 / 6)
- Ideas needed for problem with trailing whitespaces (C++)
- loop problem (C++)
- Help with Arrays needed! (C++)
- while loop in c++ help needed (C++)
- JScript experts - HELP needed! (Java)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Show Nav image on mouse over text link
- Next Thread: Javascript opener with flash target blank
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxhelp animate array automatically beta box bug calendar captchaformproblem cart checkbox child class codes column cookies createrange() css cursor decimal design disablefirebug dom download dropdown editor element engine enter error events explorer ext file firefox focus forms frameworks google gwt html htmlform ie8 iframe image() images index internet java javascript jawascriptruntimeerror jquery jsf jsfile jsp jump listbox masterpage math menu microsoft mimic mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent pdf php player post problem progressbar prototype redirect regex runtime scale scroll search select session shopping size sql text textarea toggle w3c web website window windowofwords windowsxp wysiwyg \n





