View Full Version : Need help modify code

10-03-2011, 07:44 PM
Hi need help with a loan calculator script, i know the moderator frowns on such requests but i just have to, im willing 2 buy a cup of coffee if it works, i want the code below to display the total loan repayment amount instead of the monthly payment payment = principle * monthly interest/(1 - (1/(1+MonthlyInterest)*Months)) , eg if if l0an is 20$ payment is in 4 months, intrest rate is 15% display = amount to be repaid. thanks

<script language="JavaScript">
function showpay() {
if ((document.calc.loan.value == null || document.calc.loan.value.length == 0) ||
(document.calc.months.value == null || document.calc.months.value.length == 0)
(document.calc.rate.value == null || document.calc.rate.value.length == 0))
{ document.calc.pay.value = "Incomplete data";
var princ = document.calc.loan.value;
var term = document.calc.months.value;
var intr = document.calc.rate.value / 1200;
document.calc.pay.value = princ * intr / (1 - (Math.pow(1/(1 + intr), term)));

// payment = principle * monthly interest/(1 - (1/(1+MonthlyInterest)*Months))


// -->

The results of this loan payment calculator are for comparison purposes only.
They will be a close approximation of actual loan
repayments if available at the terms entered, from a financial institution. This
is being
provided for you to plan your next loan application. To use, enter values
for the
Loan Amount, Number of Months for Loan, and the Interest Rate (e.g.
7.25), and
click the Calculate button. Clicking the Reset button will clear entered
<form name=calc method=POST>
<table width=60% border=0>
<tr><th bgcolor="#aaaaaa" width=50%><font color=blue>Description</font></th>
<th bgcolor="#aaaaaa" width=50%><font color=blue>Data Entry</font></th></tr>
<tr><td bgcolor="#eeeee">Loan Amount</td><td bgcolor="#aaeeaa" align=right><input
type=text name=loan
<tr><td bgcolor="#eeeee">Loan Length in Months</td><td bgcolor="#aaeeaa"
align=right><input type=text
name=months size=10></td></tr>
<tr><td bgcolor="#eeeee">Interest Rate</td><td bgcolor="#aaeeaa" align=right><input
type=text name=rate
<tr><td bgcolor="#eeeee">Monthly Payment</td><td bgcolor="#eeaaaa"
align=right><em>Calculated</em> <input
type=text name=pay size=10></td></tr>
<tr><td bgcolor="#aaeeaa"align=center><input type=button onClick='showpay()'
value=Calculate></td><td bgcolor="#eeeeaa" align=center><input type=reset
<font size=1>Enter only numeric values (no commas), using decimal points
where needed.<br>
Non-numeric values will cause errors.</font>

<p align="center"><font face="arial" size="-2">This free script provided by</font><br>
<font face="arial, helvetica" size="-2"><a href="http://javascriptkit.com">JavaScript

10-03-2011, 08:05 PM
What's the expected answer for ? (I have the script ready but since I'm not too smart in Algebra ;) I might mess up):

eg if if l0an is 20$ payment is in 4 months, intrest rate is 15%

Old Pedant
10-03-2011, 09:05 PM

It's terribly out of date. It was written in 1998 for a contest where you were only allowed a maximum of 5k bytes to create an HTML page, so to make it fit it was compressed to the point where it's hard to read. And, of course, the JavaScript is designed for MSIE 4 and Netscape 4.

Still, it works, and works well.

Old Pedant
10-03-2011, 09:21 PM
To change the original code just do the following:

In the <form> part of the HTML add in one row, right after the payment row, as shown in red here:

<td bgcolor="#eeeee">Monthly Payment</td>
<td bgcolor="#eeaaaa" align=right>
<input type=text name=pay size=10>
<td bgcolor="#eeeee">Total of Payments</td>
<td bgcolor="#eeaaaa" align=right>
<input type=text name=total size=10>

(Notice that I did not attempt to change the HTML to modern standards. I left it as is.)

And then make the changes in red in the JS part of the code;

var princ = document.calc.loan.value;
var term = document.calc.months.value;
var intr = document.calc.rate.value / 1200;
var pmt = princ * intr / (1 - (Math.pow(1/(1 + intr), term)));
document.calc.pay.value = pmt.toFixed(2)
document.calc.total.value = ( term * pmt ).toFixed(2);