OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why on earth won't this Javascript program run? [closed]

  • Thread starter Thread starter Rich2233
  • Start date Start date
R

Rich2233

Guest
Just trying to make a simple client-side mortgage calculator in javascript. When I try to run it, nothing happens. I assume its something really simple, but I just can't figure it out. Any ideas? I appreciate your help.

Code:
<html>
<head>
<title> Mortgage Calculator </title>

<script type = "text/javascript">
<!--
document.getElementById("mortgageCalc").calculateMortgage.onclick = CalcMortgage ();

function CalcMortgage ()
{
 var elt = document.getElementById("mortgageCalc");
 var P = elt.principal.value;
 var time = elt.interest.value;
 var n = elt.term.value;
 var isdigit = /^[-]?[0-9]+[\.]?[0-9]+$;
 
 if (isdigit.test(P) && isdigit.test(time) && isdigit.test(n))
 {
    var r = (time/100)/12;
    n = Math.round(n);
    
    var R = P*r/(1-(1/(Math.pow((1+r),n))));
    R = R.toFixed(2);
    
    var totalPayment = R*Number(n);
    totalPayment = totalPayment.toFixed(2);
    
    var totalInterest = totalPayment - P;
    totalInterest = totalInterest.toFixed(2);

    elt.monthlyPayment.value = R;
    elt.totalPayment.value = totalPayment;
    elt.totalInterest.value = totalInterest;
 }
 else
{
alert("Do not use non-numeric or negative numbers.");
}
}

// -->
</script>

</body>
</html>

</head>

<body>
<h3> Mortgage Calculator </h3>

<form id = "mortgageCalc" action = "" method = "post" >
<table border = "0">

<tr>
<td> Principal Amount: </td>
<td>
<input type = "text" name = "principal" size = "15" />
</td>
</tr>

<tr>
<td> Annual Interest Rate (%): </td>
<td>
<input type = "text" name = "interest" size = "15" />
</td>
</tr>

<tr>
<td> Loan Term (months): </td>
<td>
<input type = "text" name = "term" size = "15" />
</td>
</tr>

<tr>
<td>
<input type = "button" name = "calculateMortgage" value = "Calculate" />
</td>

<td>
<input type = "reset" value = "Clear" />
</td>
</tr>

<tr>
<td> Monthly Payments: </td>
<td>
<input type = "text" name = "monthlyPayment" value = "" size = "15" />
</td>
</tr>

<tr>
<td> Sum Total Payments: </td>
<td>
<input type = "text" name = "totalPayment" value = "" size = "15" />
</td>
</tr>

<tr>
<td> Total Interest Paid: </td>
<td>
<input type = "text" name = "totalInterest" value = "" size = "15" />
</td>
</tr>

</table>

</form>

<p>Just trying to make a simple client-side mortgage calculator in javascript. When I try to run it, nothing happens. I assume its something really simple, but I just can't figure it out. Any ideas? I appreciate your help.</p>
<pre><code><html>
<head>
<title> Mortgage Calculator </title>

<script type = "text/javascript">
<!--
document.getElementById("mortgageCalc").calculateMortgage.onclick = CalcMortgage ();

function CalcMortgage ()
{
var elt = document.getElementById("mortgageCalc");
var P = elt.principal.value;
var time = elt.interest.value;
var n = elt.term.value;
var isdigit = /^[-]?[0-9]+[\.]?[0-9]+$;

if (isdigit.test(P) && isdigit.test(time) && isdigit.test(n))
{
var r = (time/100)/12;
n = Math.round(n);

var R = P*r/(1-(1/(Math.pow((1+r),n))));
R = R.toFixed(2);

var totalPayment = R*Number(n);
totalPayment = totalPayment.toFixed(2);

var totalInterest = totalPayment - P;
totalInterest = totalInterest.toFixed(2);

elt.monthlyPayment.value = R;
elt.totalPayment.value = totalPayment;
elt.totalInterest.value = totalInterest;
}
else
{
alert("Do not use non-numeric or negative numbers.");
}
}

// -->
</script>

</body>
</html>

</head>

<body>
<h3> Mortgage Calculator </h3>

<form id = "mortgageCalc" action = "" method = "post" >
<table border = "0">

<tr>
<td> Principal Amount: </td>
<td>
<input type = "text" name = "principal" size = "15" />
</td>
</tr>

<tr>
<td> Annual Interest Rate (%): </td>
<td>
<input type = "text" name = "interest" size = "15" />
</td>
</tr>

<tr>
<td> Loan Term (months): </td>
<td>
<input type = "text" name = "term" size = "15" />
</td>
</tr>

<tr>
<td>
<input type = "button" name = "calculateMortgage" value = "Calculate" />
</td>

<td>
<input type = "reset" value = "Clear" />
</td>
</tr>

<tr>
<td> Monthly Payments: </td>
<td>
<input type = "text" name = "monthlyPayment" value = "" size = "15" />
</td>
</tr>

<tr>
<td> Sum Total Payments: </td>
<td>
<input type = "text" name = "totalPayment" value = "" size = "15" />
</td>
</tr>

<tr>
<td> Total Interest Paid: </td>
<td>
<input type = "text" name = "totalInterest" value = "" size = "15" />
</td>
</tr>

</table>

</form>
</code></pre>
 

Latest posts

Top