Post by a'Lan Mandragoran on Aug 5, 2008 17:52:03 GMT -5
Ok.... I'm sitting here and I am really really friggen annoyed that something I'm working on isn't working.
I am trying to teach myself JavaScript and I got the O'Reilly book "JavaScript 5th ed." and I'm trying to do one of the goofy little "try it yourself" things. I typed it out exactly as it said to and it didn't work and then I broke it into different files like you're supposed to. I have NO clue what's wrong and I've posted this on other boards so I might as well post it here too.
CODE FOLLOWS!
This is from the HTML file
Now here is the JS file.
The issue is that what it should display, dosn't appear at all.
Test number set is as follows
Loan Amount: 200000
interest rate: 6.5
years: 30
Results are supposed to be
Monthly payment: $1264.14
Total Payment: $455088.98
Total Interest: $255088.98
I almost want to go to school to get help but I want to try and figure this out on my own.
I am trying to teach myself JavaScript and I got the O'Reilly book "JavaScript 5th ed." and I'm trying to do one of the goofy little "try it yourself" things. I typed it out exactly as it said to and it didn't work and then I broke it into different files like you're supposed to. I have NO clue what's wrong and I've posted this on other boards so I might as well post it here too.
CODE FOLLOWS!
This is from the HTML file
<html>
<head>
<script language="JavaScript" type="text/javascript" src="payment.js"></script>
<link rel="stylesheet" type="text/css" href="payment.css">
<title>Loan</title>
</head>
<body>
<!--
Table name is "loandata" and the field names will be used by JavaScript to fill in or retrieve information in the table.
-->
<form name="loandata">
<table>
<tr>
<td><b>Enter Loan Information:</b></td>
<td></td>
</tr>
<tr>
<td>1) Amount of loan </td>
<td><input type="text" name="principal" onchange="calculate();"/></td>
</tr>
<tr>
<td>2) Anual percentace rate of interest: </td>
<td><input type="text" name="interest" onchange="calculate();"/></td>
</tr>
<tr>
<td>3) Repayment period in years: </td>
<td><input type="text" name="years" onchange="calculate();"/></td>
</tr>
<tr>
<td></td>
<td><div align="right"><input type="button" value="Compute" onclick="calculate();" /></div></td>
</tr>
<tr>
<td><strong>Payment Information:</strong>
</td>
<td></td>
</tr>
<tr>
<td>4) Your monthly payments:</td>
<td class="result">$<span id="payment"></span></td>
</tr>
<tr>
<td>5) Your Total Payment : </td>
<td class="result">$<span id="total"></span></td>
</tr>
<tr>
<td>6) Your total interest payments: </td>
<td class="result">$<span id="totalinterest"></span></td>
</tr>
</table>
</form>
</body>
</html>
Now here is the JS file.
/*
*This is the JavaScript that goes along with an XHTML page to calculate payments and intrest on loans.
*This defines the calculate() funtion used in the page.
*/
function calculate()
{
//obtain information assuming all fields are valid.
//converts annual rate to monthly rate
//converts payment period in year to number of monthly payments
var principal = document.loandata.principal.value;
var interest = document.loandata.interet.value /100/12;
var payments = document.loandata.payments.value * 12;
//compute monthly payment figure using esoteric math.
var x = Math.pow(1 + interest, payments);
var monthly = (principal * x * interest)/(x - 1);
//get named <span> elements from form
var payment = document.getElementById ("payment");
var total = document.getElementById ("total");
var totalinterest = document.getElementById ("totalinterest");
//check that the result is a finite number and display if true
if (isFinite(monthly))
{
payment.innerHTML = monthly.toFixed(2);
total.innerHTML = (monthly * payments).toFixed(2);
totalinterest.innerHTML = ((monthly * payments)-principal).toFixed(2);
}
//if false, nothing will display. Assume invalid input
else
{
payment.innerHTML = "broken";
total.innerHTML = "broken";
totalinterest.innerHTML = "broken";
}
}
The issue is that what it should display, dosn't appear at all.
Test number set is as follows
Loan Amount: 200000
interest rate: 6.5
years: 30
Results are supposed to be
Monthly payment: $1264.14
Total Payment: $455088.98
Total Interest: $255088.98
I almost want to go to school to get help but I want to try and figure this out on my own.