简体   繁体   中英

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

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

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

conBalance does not get a value

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM