| | |
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 |
ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate array automatically browser bug calendar captchaformproblem cart checkbox child close codes createrange() cursor date debugger dependent disablefirebug dom dropdown editor element embed engine events explorer ext file firefox focus form forms frameworks getselection google gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe images internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jsp jump libcurl maps masterpage math media microsoft object onmouseoutdivproblem onreadystatechange parent paypal pdf php player position post programming progressbar prototype redirect regex runtime safari scale scriptlets scroll search security shopping size software sql text textarea unicode web website windowsxp wysiwyg \n





