I am using the following function to get information on loan payments
I would like to pass through about 7 different accounts and then total the interest paid for all or however many I need
I am really a bit lost when it comes to arrays and quite frankly need even sure if that is what I want to do, I think it is. Any help or insight would be appreciated
Thanks
Code:
function myPaymentCalc(interestRate,monthlyPayment,principal)
{
accumulatingInterest = 1;
if (! validInt(interestRate) || ! validInt(monthlyPayment) ||
! validInt(principal))
return(false);
if (interestRate == 0 || monthlyPayment == 0 || principal == 0) {
alert("You need to specify your monthly payment, credit card " +
"interest rate and current debt amount before I can calculate " +
"how long it'll take for you to pay off your debt.");
return(false);
}
interestRate = (interestRate > 1)? interestRate / 100.0 : interestRate;
monthlyInterestRate = interestRate / 12;
if (principal * monthlyInterestRate > monthlyPayment) {
monthlyrate = "$" + centNotation(principal * monthlyInterestRate);
permonthPayment = "$" + centNotation(monthlyPayment);
alert("You've got a fundamental problem: your monthly payment of " +
permonthPayment + " is less than the interest added each month " +
"(which is " + monthlyrate + ") and you will never " +
"pay off this debt. It might be a good time to call your creditors.");
return(false);
}
interimVal1 = monthlyPayment - (monthlyInterestRate * principal)
numberOfPayments = (Math.log(monthlyPayment) - Math.log(interimVal1)) /
Math.log(1+monthlyInterestRate);
numberOfPayments = Math.ceil(numberOfPayments);
interimVal2 = Math.pow((monthlyInterestRate+1), (numberOfPayments-1));
remaining = (principal * interimVal2) -
(monthlyPayment/monthlyInterestRate) * (interimVal2 - 1);
totalPayment = monthlyPayment *(numberOfPayments-1) + remaining;
payments = numberOfPayments;
totalpayment = formatNumber(totalPayment);
interestPaid = formatNumber(totalPayment-principal);
But your monthlyInterestRate = interestRate / 12; is wrong. The monthlyInterestRate (in %) should verify : Math.pow(1+monthlyInterestRate/100,12) = 1+interestRate/100; Then it is given by Math.pow(1+interestRate/100,1/12) -1 which is a always little lower. You work as bankers !
Last edited by 007julien; 02-19-2013 at 12:19 PM..
But your monthlyInterestRate = interestRate / 12; is wrong. The monthlyInterestRate (in %) should verify : Math.pow(1+monthlyInterestRate/100,12) = 1+interestRate/100; Then it is given by Math.pow(1+interestRate/100,1/12) -1 which is a always little lower. You work as bankers !
Thanks I will look into that, but I doesnt address the question in my post
I don't see any array in your code. But you declare an array with var myarray = [], and then you can store values in each array index (remember the array index starts at 0) , and ultimately total them using a loop.
Remember that alerts are regarded as obsolete and should only be used for testing. Use DOM methods to display a message to your users.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
I don't see any array in your code. But you declare an array with var myarray = [], and then you can store values in each array index (remember the array index starts at 0) , and ultimately total them using a loop.
Remember that alerts are regarded as obsolete and should only be used for testing. Use DOM methods to display a message to your users.
Yes I know there isnt an array
Quote:
I am really a bit lost when it comes to arrays and quite frankly need even sure if that is what I want to do, I think it is. Any help or insight would be appreciated
That function takes one set of variables I would like to use it to pass several and store the result of each interest calculation then add them together and have the answer displayed on my document
That function takes one set of variables I would like to use it to pass several and store the result of each interest calculation then add them together and have the answer displayed on my document
You do realise that you cannot store values in this way to use in some other session? In other words, you would have to run your script several times in a loop, adding the calculated values to the array each time, and finally totalling them. But without cookies or local storage (or of course a server-side database) the contents of the array are lost when the program ends.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.