Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: Return array of values ?

1. Return array of values ?

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) {
"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);
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);```

2. Simply return [payments,totalpayment,interestPaid]; ?

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 !

3. Originally Posted by 007julien
Simply return [payments,totalpayment,interestPaid]; ?

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

4. 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.

5. Originally Posted by Philip M
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

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

6. Yes. We are of the same opinion... I give the response without question !

See for example the Peter-Paul Koch's function findPosition which return two value with an Array.

An other way to return an Array consist to use the slide method (return myArray.slide(0)) which return a copy of an array...

7. Originally Posted by 007julien
Yes. We are of the same opinion... I give the response without question !

See for example the Peter-Paul Koch's function findPosition which return two value with an Array.

An other way to return an Array consist to use the slide method (return myArray.slide(0)) which return a copy of an array...
I assume you mean slice function

8. Yes Obviously I do ! Thanks

9. Originally Posted by billboy
Yes I know there isnt an array

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.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•