Ian
Ian

Reputation: 865

What am I missing on this javascript loan calculator?

I am trying to build a very simple loan calculator in js where the user enters an amount, an interest rate and either a monthly payment or a length of time to pay the loan and then the calculator is supposed to show the results with a monthly payment amount, a total time for the loan and a total repayment amount including interest.

I'm sure my math is correct on this, but it won't seem to calculate. I did have it working in a previous version, so I think I might be missing something. Hopefully, one of you reading this is eagle eyed enough to tell me what I've done wrong.

Here's the code: http://jsfiddle.net/LsJ5U/

Thanks in advance for any help you guys can offer.

Cheers,

Ian

Upvotes: 1

Views: 992

Answers (4)

GillesC
GillesC

Reputation: 10874

Your conBalance collection is wrong, you are trying to extract the innerTEXT when what you want is the value. Because this is wrong your conBalance is an empty string (as innerTEXT is empty for that element.

So you should have

var conBalanceElement = document.getElementById('conBalance');
var conBalance = conBalanceElement.value;

Instead of

var conBalanceElement = document.getElementById('conBalance');
var conBalance ;
if (document.all) {
    conBalance = conBalanceElement.innerText;
}
else {
    conBalance = conBalanceElement.textContent;
}

In your fiddle example doing those changes was enough to generate the monthly repayment and the total repayment (didn't check the maths as you seems confident enough with them)

Upvotes: 1

SergeS
SergeS

Reputation: 11779

http://jsfiddle.net/LsJ5U/6/ - here it is fixed , use value for getting total loan.

PS Also it is good practice to create a function which will put results into html, instead of 4 times copied script

Upvotes: 0

Steen
Steen

Reputation: 2877

conBalance does not get a value

this worked: var conBalance = document.getElementById('conBalance').value;

Upvotes: 1

Luan
Luan

Reputation: 402

You should use .value to get the value of the first text field.

Here is the updated fiddle. http://jsfiddle.net/luan/LsJ5U/4/

Upvotes: 2

Related Questions