Click to See Complete Forum and Search --> : new to html / Jscrpit


z_mirza
01-19-2003, 02:16 PM
can any one figure out whats wrong with this page/script if its in bad programing style well exuse me! im a newbie at this, 3d graphics is really my thing but i wanted to get into webdev also, thanx in adv,

<html>
<head>
<title>JavaScript Loan Calculators</title>
</head>
<body>
<form name="loandata">
<table>
<tr>
<td colspan= "3"><b>Enter Loan Informatoin:</b>
</td>
</tr>


<tr>
<td>1)</td>
<td>Amount of the loan (any currency):</td>
<td><input type="text" name="principal" size="12"
onchange="calculate();"></td>
</tr>


<tr>
<td>2)</td>
<td>Annual percentage rate of interest:</td>
<td><input type="text" name="interest" size="12"
onchange="calculate();"></td>
</tr>


<tr>
<td>3)</td>
<td>Repayment period in years:</12>
<td><input type="text" name="years" size="12"
onchange="calculate();"></td>
</tr>


<tr>
<td colspan="3">
<input type="button" value="Compute" onclick="calculate();">
</td>
</tr>


<tr>
<td colspan="3">
<b>Payment Informatoin:</b>
</td>
</tr>


<tr>
<td>4)</td>
<td>Your monthly payment will be:</td>
<td><input type="text" name="payment" size="12"><td>
</tr>


<tr>
<td>5)</td>
<td>Your total payment will be:</td>
<td><input type="text" name="total" size="12"><td>
</tr>


<tr>
<td>4)</td>
<td>Your interest payments will be:</td>
<td><input type="text" name="totalinterest" size="12"><td>
</tr>
</table>
</form>



<script language="JavaScript">
function calculate()
{
var principal = document.loandata.principal.value;
var interest = document.loandata.interest.value / 100 / 12;
var payments = document.loandata.years.value * 12;

var x = Math.pow(1 + interest, payments);
var monthly = (principal*x*inerest)/(x-1);


if(!isNaN(monthly) &&
(monthly != Number.POSITIVE_INFINITY) &&
(monthly != Number.NEGATIVE_INFINITY))
{
document.loandata.payment.value = round(monthly);
document.loandata.total.value = round(monthly * payments);
document.loandata.totalinterest.value = round((monthly *payments) - principal);
}

else
{
document.loandata.payment.value = "";
document.loandata.total.value = "";
document.loandata.totalinterest.value = "";
}
}


function round(x)
{
return Math.round(x*100)/100;
}

</script>
</body>
</html>


edit: i know its a simple mistake but i cant find it :confused:

khalidali63
01-19-2003, 02:46 PM
What is it you intend this page to do for you.?
please describe the problem.
Khalid

z_mirza
01-19-2003, 02:51 PM
the program computes the monthly payment on a loan given the amount of the loan the interest rate and the repayment period. the form elements are embedded in a table. the forms name is loandata. after entering the 1st 3 fields and clicking compute it should well compute the 2nd set of fields and display what the numbers should be. simple program simple mistake some where in the program. my not so simple mind cant figure it out lol so help me out guys
thx

khalidali63
01-19-2003, 02:52 PM
Well here is the obvious errors fix

replace this function in it entirety
calculate()

with this


function calculate(){
var principal = document.loandata.principal.value;
var interest = (parseInt(document.loandata.interest.value) / 100) / 12;
var payments = document.loandata.years.value * 12;

var x = Math.pow(1 + interest, payments);
var monthly = (principal*x*interest)/(x-1);


if(!isNaN(monthly) &&
(monthly != Number.POSITIVE_INFINITY) &&
(monthly != Number.NEGATIVE_INFINITY)){
document.loandata.payment.value = round(monthly);
document.loandata.total.value = round(monthly * payments);
document.loandata.totalinterest.value = round((monthly *payments) - principal);
}else{
document.loandata.payment.value = "";
document.loandata.total.value = "";
document.loandata.totalinterest.value = "";
}
}



cheers

Khalid

z_mirza
01-19-2003, 03:03 PM
lol yea i figured it out lol i didnt have the 1st T in interest thanx man