What did you write this in? You have typos and formatting errors all over. Use the Firefox Error console for this type of stuff, it would take you a couple seconds, tells you exactly where the error is. When I copied your code to a text editor it gave me warnings about characters in a different format, if you look at your code it shows up a couple spots with
_strPennies
or
_rate
when these should be strPennies and rate . Also the subtraction symbols showed up in the browser as unrecognized characters, so I just replaced them and saved and it worked.
A working copy of your code: (the only things changed were subtraction symbols, a couple line breaks, and the couple random underscores)
CODE
<HTML>
<HEAD>
<TITLE>Loan Calculator</TITLE>
<script LANGUAGE="JavaScript">
<!-- hide from non-JavaScript browsers
function roundToPennies(n)
{
pennies = n * 100;
pennies = Math.round(pennies);
strPennies = new String(pennies);
len = strPennies.length;
return strPennies.substring(0, len - 2) + "." +
strPennies.substring(len - 2, len);
}
function Monthly(principal, years, apr)
{
rate = apr / 12;
payments = years * 12;
return roundToPennies( principal * rate / (1 - (1 / Math.pow(1 + rate, payments) ) ) );
}
function MonthlyAmortization(principal, years, apr)
{
payments = years * 12;
monthlyInterest = apr / 12;
monthlyPayment = Monthly(principal, years, apr);
document.write("<CENTER>");
document.write("<H1>Amortization Table</H1>");
document.write("<HR>");
document.write("<TABLE BORDER>");
document.write("<TR>");
document.write("<TH COLSPAN=4>");
document.write("$" + roundToPennies(principal));
document.write(" at " + roundToPennies(apr*100) + "%");
document.write(" over " + years + " years.<BR>");
document.write("Monthly payment: $" + Monthly(principal, years, apr));
document.write("</TH>");
document.write("</TR>");
document.write("<TR>");
document.write("</TH>");
document.write("<TH COLSPAN=2>Payment</TH>");
document.write("</TR>");
document.write("<TR>");
document.write("Month</TH>");
document.write("Interest</TH>");
document.write("Principal</TH>");
document.write("Balance</TH>");
document.write("</TR>");
for(i = 1; i <= payments; i++)
{
document.write("<TR>");
document.write("<TD>" + i + "</TD>");
interestPayment = principal * monthlyInterest;
document.write("<TD>$" + roundToPennies(interestPayment) + "</TD>");
principalPayment = monthlyPayment - interestPayment;
document.write("<TD>$" + roundToPennies(principalPayment) + "</TD>");
principal -= principalPayment;
document.write("<TD>$" + roundToPennies(principal) + "</TD>");
document.write("</TD>");
}
document.write("</TABLE>");
document.write("</CENTER>");
}
function compute(form)
{
if((form.principal.value.length != 0) &&
(form.apr.value.length != 0) &&
(form.years.value.length != 0))
{
principal = eval(form.principal.value);
apr = eval(form.apr.value) / 100.0;
years = eval(form.years.value);
if(years == 0.0)
{
alert(
"You have no monthly payment, since the number of years is zero.");
}
else
{
MonthlyAmortization(principal, years, apr);
}
}
else
{
alert("You must fill in all the fields.");
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<CENTER><H1>Loan Calculator</H1></CENTER>
<HR>
<CENTER>
<FORM>
<CENTER>
Fill in the fields, then click
<INPUT TYPE=BUTTON VALUE="Amortize!" onClick=compute(this.form)>
</CENTER>
<P>
<TABLE BORDER=3>
<TR>
<TD>Amount of the loan ($)</TD>
<TD><INPUT TYPE=TEXT NAME=principal></TD>
</TR>
<TR>
<TD>Annual interest rate (%)</TD>
<TD><INPUT TYPE=TEXT NAME=apr></TD>
</TR>
<TR>
<TD>Total number of years</TD>
<TD><INPUT TYPE=TEXT NAME=years></TD>
</TR>
</TABLE>
</FORM>
</CENTER>
</BODY>
</HTML>