What am I missing on this javascript loan calculator?

950 views Asked by At

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

4

There are 4 answers

0
GillesC On BEST ANSWER

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)

1
Luan On

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

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

0
Steen On

conBalance does not get a value

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

1
SergeS On

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