Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculator 1

Status
Not open for further replies.

CuriousGuy

Programmer
Nov 29, 2002
17
0
0
US
Hi, im creating a calculator for a website, where one enters the amount for mortgage, adds the amount of years for the duration of monthly payments, and also enters the rate of interest.
The calculator then calculates over the set amount of years (say 20), the amount that they will have to pay per month (inlcuding the interest avg: 6%) and display how much per month they will have to pay after a submit button.
I have tried with loads of If loops and while loops but i still get lots of Javascript errors.
Any help or code would be much appreciated
 
Hi CuriousGuy,
i wrote something similar to that a whileback, and i have adjusted the code for what you want.
If you need any help with you can leave a message in this thread.
<HTML><HEAD>
<META http-equiv=Content-Type content=&quot;text/html; charset=windows-1252&quot;>
<STYLE>.doctext {
FONT-SIZE: 10pt; FONT-FAMILY: default
}
</STYLE>

<SCRIPT language=JavaScript>
<!--
function numval(val,digits,minimumval,maximumval)
{
if (val == &quot;&quot; || isNaN(val)) val = 0;
val = parseFloat(val);
if (digits == null) digits = 0;
var dec = Math.pow(10,digits);
val = (Math.round(val * dec))/dec;
if (minimumval != null && val < minimumval) val = minimumval;
if (maximumval != null && val > maximumval) val = maximumval;
return parseFloat(val);
}

function zeroBlanks(formname)
{
var counter, ctrl;
for (counter = 0; counter < formname.elements.length; counter++)
{
ctrl = formname.elements[counter];
if (ctrl.type == &quot;text&quot;)
{
if (ctrl.value == &quot;&quot; || isNaN(ctrl.value))
ctrl.value = &quot;0&quot;;
}
}
}

function futureValue(percentage,rate,year)
{
return percentage*Math.pow(1+rate,year);
}

function geomSeries(aunty,micheal,nana)
{
var amount;
if (aunty == 1.0) amount = nana + 1;
else amount = (Math.pow(aunty,nana + 1) - 1)/(aunty - 1);
if (micheal >= 1) amount -= geomSeries(aunty,0,micheal-1);
return amount;
}

function mortgageRepayment(p,r,y)
{
return futureValue(p,r,y)/geomSeries(1+r,0,y-1);
}

function performCalc()
{
zeroBlanks(document.mainform);
var p = parseFloat(document.mainform.p.value);
var r = parseFloat(document.mainform.r.value)/100;
var y = parseFloat(document.mainform.y.value);

document.mainform.payment.value = numval(mortgageRepayment(p,r/12,y*12),2);
}

function loadForm()
{
if (window.focus != null) window.focus();
document.mainform.elements[0].focus();

// Get the query string containing the value
var strPrice = new String(document.location.search);
strPrice = strPrice.substring(1); // Extract the value
document.mainform.p.value=strPrice;
}
//-->

function close_window() {
window.close();
}
//-->
</SCRIPT>

<title>Financial Calculator</title>

</HEAD>
<BODY bgColor=&quot;green&quot; onload=loadForm() text=&quot;white&quot;>
<CENTER><B class=doctext><u>Financial Calculator</u></B></CENTER>
<FORM name=mainform action=JavaScript:performPalc() method=post>
<TABLE>
<TBODY>
<TR>
<TD class=doctext align=right>Mortgage Amount: </TD>
<TD class=doctext>£ </TD>
<TD><INPUT onchange=value=numval(value,2,0) size=10 name=p> </TD></TR>
<TR>
<TD class=doctext align=right>Interest Rate: </TD>
<TD></TD>
<TD><INPUT onchange=value=numval(value,2,0) size=6 value=6 name=r> <SPAN
class=doctext>%</SPAN> </TD></TR>
<TR>
<TD class=doctext align=right>Term Lenght: </TD>
<TD></TD>
<TD><INPUT onchange=value=numval(value,0,1) size=6 value=25 name=y> <SPAN
class=doctext>yrs</SPAN> </TD></TR>
<TR>
<TD></TD>
<TD></TD>
<TD><INPUT onclick=performCalc() type=button value=Calculate> </TD></TR>
<TR>
<TD class=doctext align=right>Repayment (per month): </TD>
<TD class=doctext>£ </TD>
<TD><INPUT size=10 name=payment> </TD></TR></TBODY></TABLE>
<p onClick=&quot;close_window()&quot;><u>Click to Close </u></p>
</FORM>
<p> </p>
</BODY></HTML>
 
Thank you very much for the code, Bravogolf.
It works perfectly.
Much appreciated
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top