# WEB Help with another javascript homework problem?

Discussion in 'OT Technology' started by Kirk Hinrich, Jun 2, 2008.

1. ### Kirk HinrichNew Member

Joined:
Sep 20, 2004
Messages:
40,492
0
Location:
Brown Line
Here is my problem:
Compute the Monthly Payment for a Loan
Write a Javascript program that computes monthly interest payments for a home or car loan. You must use text boxes to get the input from the user.
Inputs = Loan amount (principal) r = Interest rate in percent n = Number of years (duration of loan) Output:M = Monthly payment

Here is the formula:
M=Pr / 1200.01 – (1.0 + r / 1200.0) –12n
• Use parseFloat to convert the inputs into numbers.
• Use a function (with a name like computeMonthlyPayment) to compute the monthly payment M.
• Use the following JavaScript formula for computing the monthly payment: M = (P * r / 1200.0) / (1 - Math.pow(1.0 + r / 1200.0, -12.0 * n));

--------------------------------------------------------------------------

Here is the code I've got so far

<html>
<title>JavaScript Loan Example</title>
<script type="text/javascript">
function compute(){
rate =document.frmcalc.txtRate.value;
principal=document.frmcalc.txtPrince.value;
year=document.frmcalc.txtYears.value;

</script>
<body>
<form name="frmcalc">
<input type="text" name="txtRate" size="7" value="" />
<input type="text" name="txtPrince" size="7" value="" />
<p>Type the duration of your loan in years:<p>
<input type="text" name="txtYears" size="7" value="" />
</br>
<input type="button" value="Click to calculate your loan" onclick="compute();">
<P> Your loan payment is <input type="text" size="7" name="res" value="">
</form>
</body>
</html>

--------------------------------------------------------------------------

I've kind of been doing it backwards, going back and doing some things that I know how to set up while leaving parts blank
My teacher kind of sucks I especially don't understand how to "parse" the numbers

Any help is appreciated

2. ### Slid.I'm a guy.

Joined:
Oct 25, 2001
Messages:
1,928
0
Location:
NH
* Use parseFloat to convert the inputs into numbers.

Where you have: rate =document.frmcalc.txtRate.value;
You should have: rate = parseFloat(document.frmcalc.txtRate.value);

* Use a function (with a name like computeMonthlyPayment) to compute the monthly payment M.

* Use the following JavaScript formula for computing the monthly payment: M = (P * r / 1200.0) / (1 - Math.pow(1.0 + r / 1200.0, -12.0 * n));

Now you just need to replace his variables (M, P, r, n) with yours.

P = principal
r = rate
n = year

So you end up with:

M = (principal * rate / 1200.0) / (1 - Math.pow(1.0 + rate / 1200.0, -12.0 * year));

You'd want to end your function with an alert or something to return the monthly payment:

Edit:
Instead of alert(M); you want it in a text box? So you do:

document.frmcalc.res.value = M;

3. ### Kirk HinrichNew Member

Joined:
Sep 20, 2004
Messages:
40,492
0
Location:
Brown Line
hmmm ok
I think I'm getting somewhere

here is what I've got now in the function but I am still getting errors

function compute(){
rate = parseFloat(document.frmcalc.txtRate.value);
principal=parseFloat(document.frmcalc.txtPrince.value);
year=parseFLoat(document.frmcalc.txtYears.value);
M = (principal * rate / 1200.0) / (1 - Math.pow(1.0 + rate / 1200.0, -12.0 * year));
}

4. ### Slid.I'm a guy.

Joined:
Oct 25, 2001
Messages:
1,928
0
Location:
NH
Okay, a few problems in your code like random spaces and improper casing:

principal=parseFloat(document.frmcalc.txtPrince.va lue);
year=parseFLoat(document.frmcalc.txtYears.value);

The following works, I put the alert('test') in to see where it was breaking. You'll want to remove those. You'll likely want to use a function to set the decimal point (precision) to two. You can learn more about that here: http://www.mredkj.com/javascript/nfbasic2.html

Code:
```<html>
<title>JavaScript Loan Example</title>
<script type="text/javascript">

function computeLoan(){

rate = parseFloat(document.frmcalc.txtRate.value);
principal=parseFloat(document.frmcalc.txtPrince.value);
year=parseFloat(document.frmcalc.txtYears.value);

M = (principal * rate / 1200.0) / (1 - Math.pow(1.0 + rate / 1200.0, -12.0 * year));

document.frmcalc.res.value = M;

}

</script>
<body>
<form name="frmcalc">
<input type="text" name="txtRate" size="7" value="" />
<input type="text" name="txtPrince" size="7" value="" />
<p>Type the duration of your loan in years:<p>
<input type="text" name="txtYears" size="7" value="" />
</br>
<input type="button" value="Click to calculate your loan" onclick="computeLoan();">
<P> Your loan payment is <input type="text" size="7" name="res" value="">
</form>
</body>
</html>
```

Joined:
Sep 20, 2004
Messages:
40,492