# Loan Calculator not returning correct payment

Page 1 of 1

## 3 Replies - 959 Views - Last Post: 06 July 2012 - 10:12 AM

### #1 synlight

Reputation: 89
• Posts: 582
• Joined: 14-September 11

# Loan Calculator not returning correct payment

Posted 06 July 2012 - 09:16 AM

This is a simple script for a beginning Javascript class. My question is about my function. It returns the payment as essentially half of the loan amount, instead of a valid monthly payment. The formula was given to us by our prof, and it's hard for me to troubleshoot it because I don't understand it. I have tried reading other examples online, but they look very similar to what I have. I have tested the values going in, and they are correct inside the function. Please excuse my clunky formatting, I am using what our book uses. I know there are better ways, I just don't know them yet.

<html>
<title>Chapter 6 Assignment 2</title>
<script type = "text/javascript">
function calcPayment(monthInt, months, loan){
var base = Math.pow(1+monthInt,months);
var payment = (loan*monthInt)/(1-(1/base));
return payment;}
</script>

<body bgcolor = "lightpink">
<h1>Loan Calculator</h1>
<pre>
<script type = "text/javascript">

var input = prompt("Enter the vehicle price (Ex:10000):","");
var price = parseInt(input);

input = prompt("Enter the down payment amount (Ex: 1000):","");
var down = parseInt(input);

input = prompt("Enter the annual interest rate for the loan (Ex:7.5)","");
var interest = parseFloat(input);

var loan = price - down;
var monthInt = interest/12;

document.write("Vehicle Price: \$"+price+"<br>");
document.write("Down Payment: \$"+down+"<br>");
document.write("Interest Rate: "+interest+"%<br>");
document.write("Loan Amount: \$"+loan+"<br>");

var payment = calcPayment(monthInt, 24, loan);
document.write("# of Months		Payment/month<br>");
document.write("  24			"+payment+"<br>");
payment = calcPayment(monthInt, 36, loan);
document.write("  36			"+payment+"<br>");
payment = calcPayment(monthInt, 48, loan);
document.write("  48			"+payment+"<br>");
payment = calcPayment(monthInt, 60, loan);
document.write("  60			"+payment+"<br>");

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

This post has been edited by synlight: 06 July 2012 - 09:17 AM

Is This A Good Question/Topic? 0

## Replies To: Loan Calculator not returning correct payment

### #2 ThrowsException

Reputation: 33
• Posts: 83
• Joined: 21-February 12

## Re: Loan Calculator not returning correct payment

Posted 06 July 2012 - 09:46 AM

I ran this code and didn't see any issues with it. Could you give some values that you think are giving you trouble. Have you tried debugging this in firebug or chrome developer tools?

### #3 synlight

Reputation: 89
• Posts: 582
• Joined: 14-September 11

## Re: Loan Calculator not returning correct payment

Posted 06 July 2012 - 09:54 AM

ThrowsException, on 06 July 2012 - 11:46 AM, said:

I ran this code and didn't see any issues with it. Could you give some values that you think are giving you trouble. Have you tried debugging this in firebug or chrome developer tools?

No, I've only used the error consoles in Firefox and IE. There are no errors, it seems to be a logic issue. The problem I am having is this:

Loan Amt = 10000
Down Payment = 1000
Interest = 7.5

It's returning my monthly payment at over \$5000:
Vehicle Price: \$10000
Down Payment: \$1000
Interest Rate: 7.5%
Loan Amount: \$9000
# of Months		Payment/month
24			5625.048937923487
36			5625.000144344881
48			5625.000000425756
60			5625.000000001256

....wait...... maybe I am doing my interest wrong? Shouldn't I make it .075 instead of 7.5?

Woo!! I did it! Gah, it's always something simple, isn't it? Spent 4 hours of my life on this crap LOL

### #4 ThrowsException

Reputation: 33
• Posts: 83
• Joined: 21-February 12

## Re: Loan Calculator not returning correct payment

Posted 06 July 2012 - 10:12 AM

That'll do it. Glad you got it working.