[source code] bank loan calculator
Author |
Message |
krishon
|
Posted: Mon Jun 16, 2003 10:54 am Post subject: [source code] bank loan calculator |
|
|
here's a little something i found. its from "The Complete Idiot's Guide to Javascript." If u've learned the basics of javascript, u can use this example to see how u can apply all those things about functions etc. remember this is a webpage, so if u wanna test it, copy the code into a notepad file and save as a ".html" file, then enjoy. Here's the code:
code: |
<html>
<head>
<title>Loan Calculator</title>
<script language="LiveScript">
<!--hide from non-javascript browsers
fucntion roundToPennies(n)
{
pennies = n * 100 ;
pennies = Math.round(pennies) ;
strPennies = " " + 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("<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) + "%");
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></TH>");
document.write("<TH colspan=2>Payment</TH>");
document.write("</TR>");
document.write("<TR>");
document.write("<TH>Month</TH>");
document.write("<TH>Interest</TH>");
document.write("<TH>Principal</TH>");
document.write("<TH>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("</TR>");
}
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, sicne 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>
|
tell me if this dusn't work, i'll check over the code later.
P.s. is it just me or does the syntax of C++ and Javascript look very similar? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Asok
|
Posted: Mon Jun 16, 2003 1:34 pm Post subject: (No subject) |
|
|
this is JavaScript not Java, it goes in the HTML section.
moved. |
|
|
|
|
|
krishon
|
Posted: Mon Jun 16, 2003 4:07 pm Post subject: (No subject) |
|
|
ye...i wuzn't sure bout where to put it, but now i know so its all good |
|
|
|
|
|
|
|