I am new to Javascript, and coding in general, and I am trying to make a simple loan amortization table to cement my study so far.

But, I am unsure as to the best way to input the formula I need into my code. I am receiving error messages associated with that line of code in the console (Uncaught SyntaxError: Unexpected token ; ).

I figure that I must be making an error in how I am writing out the formula. I am trying to put this into the function:

I tried using the Math.pow( , ) function, but may be getting messed up there. In general, I would really appreciate feedback on this code- first time independently trying to make anything in Javascript. Many Thanks!

Code:

var principal= 100000
var annualInterest = 5
var lengthYears= 15

function monthlyPayment (principal, annualInterest, lengthYears){
var monthlyInterest = annualInterest/(12*100);
var numberOfMonths = lengthYears*12;
var monthlyPayment = principal * ((monthlyInterest/(1-Math.pow((1+monthlyInterest)), (-1*numberOfMonths)));
return monthlyPayment;
}

while (principal> 0){
monthlyInterest = (principal*monthlyInterest);
var principalPayment = (monthlyPayment-monthlyInterest);
var newBalance = (principal-principalPayment);
principal = newBalance;
}

10-12-2013, 06:45 PM

jalarie

Quote:

Originally Posted by codeveryday

Code:

function monthlyPayment (principal, annualInterest, lengthYears){
var monthlyInterest = annualInterest/(12*100);
var numberOfMonths = lengthYears*12;
var monthlyPayment = principal * ((monthlyInterest/(1-Math.pow((1+monthlyInterest)), (-1*numberOfMonths)));
return monthlyPayment;
}

Your fourth line quoted above has the wrong number of parenthesis. I see six open ones and only five close ones.

10-12-2013, 07:27 PM

JMRKER

You never call the function correctly with any parameters to decode and use.
And you are returning a variable with the same name as the function: monthlyPayment.

10-12-2013, 11:04 PM

wbport

I have been tweaking the following for several years now. The amortization table will be created by the P.I. button after the monthly amount has been created. mortgage.htm. The monthly amount is calculated around lines 75-77 and the genpi function creates the table in a new window.

10-13-2013, 04:05 PM

codeveryday

Hi All,

Thanks for the advice and tips! It is so helpful to get nudges from those who are more experienced!

(JMRKER: In that case, I thought that since I had defined the variables globally that I could use them as parameters in the function? But, I realized that I don't actually need the function, and least I don't think I do. I can just define monthlyPayment globally I think?)

(Thanks for the heads up on the missing ')' jalarie!)
(wbport: Thanks for pointing me to your project. That looks good! I am trying to stay really simple now to cement the basic concepts that I have been studying.)

After reading JMRKER's comment, I ended up just defining most of the variables globally, and trying to use them in a for loop. The code is below, and at least it puts something to the console. But, the output soon turns to infinity. I must be doing something wrong. It is possible that I am comprehending the math involved wrong, or it's a coding issue.

Would you all mind taking a look? Am I way off base here? Again, many thanks!

Code:

var principal= prompt("what is the original principal of the loan?")
var annualInterest = prompt("What is the annual interest? Use whole numbers, so 5 means 5 percent interest")
var lengthYears= prompt("How long is the loan, in years?")
var monthlyInterest = annualInterest/(1200)
var numberOfMonths = lengthYears*12;
var monthlyPayment = principal * ((monthlyInterest/(1-Math.pow((1+monthlyInterest)), (-1*numberOfMonths))));

Why do you calculate the monthlyInterest as the annualInterest/(1200)
and then overwrite it in the for...loop with (principal * monthlyInterest) ???

Then you calculate the principalPayment as the principal * monthlyInterest; ( a very large number).
What's going on here???

You might want to re-evaluate your logic and name your variables a individuals
rather than try to re-use them as a different term at a later time.

10-14-2013, 04:14 PM

codeveryday

Hey JMRKER,

I went ahead and made a new variable within the loop, called paymentInterest. This is designed to determine how much interest the user is paying each month, the interest payments should go down over time. Since the monthlyInterest is a decimal, the number should be relatively small to the principal. The principal payment variable is designed to show how much of the monthlyPayment is actually going to pay down the principal. I then try to redefine principal within the loop to be the value of the old principal minus the amount that has been paid down.

But, I must have an issue with the formula that is determining the monthly payment. I logged that to the console, and it is a very small number. It should be about 105 for a principal of 10,000- with 5% interest, over the course of 10 years. I may be getting confused with the math.pow function. I am trying to replicate this formula:http://www.1728.org/loanform.htm

Code:

var principal= prompt("what is the original principal of the loan?")
var annualInterest = prompt("What is the annual interest? Use whole numbers, so 5 means 5 percent interest")
var lengthYears= prompt("How long is the loan, in years?")
var monthlyInterest = annualInterest/(1200)
var numberOfMonths = lengthYears*12;
var monthlyPayment = principal * (monthlyInterest/((Math.pow(1+monthlyInterest), numberOfMonths)-1));
console.log(monthlyPayment)

for( var i = 0; i<=numberOfMonths; i++){
var paymentInterest = (principal*monthlyInterest);
var principalPayment = (monthlyPayment-paymentInterest);
principal-=principalPayment
console.log(principal);
}

10-15-2013, 08:49 AM

JMRKER

Here is a modification (for testing purposes). Put back in you prompts (bad form) or put in <input> tags (better form) to collect user information.
Change you variable names in places to represent more what equations represent.

Code below outputs to a screen element to see actions rather than sending to console. Personal preference.
Check some of the results by hand to see if your formula is correct.

/* following removed for testing purposes only
var principal= prompt("what is the original principal of the loan?");
var annualInterest = prompt("What is the annual interest? Use whole numbers, so 5 means 5 percent interest");
var lengthYears= prompt("How long is the loan, in years?");
*/

/* following code for testing purposes only */
var principal = 1000;
var annualInterest = 5;
var lengthYears = 2;
/* */

var rate = annualInterest/(1200);
var numberOfMonths = lengthYears*12;
var monthlyPaymentRate = principal * (rate/((Math.pow(1+rate), numberOfMonths)-1));
// console.log(monthlyPaymentRate);

var paymentInterest, principalPayment;
document.getElementById('debug').innerHTML = 'monthlyPaymentRate: '+monthlyPaymentRate+'<p>';

var str = 'Mth Principal+Interest<br>';
for( var i = 0; i<=numberOfMonths; i++){
paymentInterest = (principal*rate);
principalPayment = (rate-paymentInterest);
principal -= principalPayment;
// console.log(principal);
str += '#'+i+': '+principal.toFixed(2)+'<br>';
}
document.getElementById('debug').innerHTML += str;
</script>

</body>
</html>

BTW:
What is the reason for your aversion to putting ';' characters at the end of your statements?
You seem to be inconsistent in using them.

10-15-2013, 12:31 PM

codeveryday

Wow! That looks much better! Thanks for the continuing help on this one. I checked it out, and I still think that there is an issue with the monthlyPaymentRate formula. That number should be fairly large- as this is the entire monthly payment on the loan. The principal should go down every loop iteration until the principal is zero. I also tried putting in the monthlyPaymentRate variable in place of the rate variable in

Code:

principalPayment = (rate-paymentInterest)

. This is because I was hoping to subtract how much the user is paying in interest from the total monthly payment. We should then be able to subtract the principalPayment variable from the principal variable, and thus have the principal go down each month.

As far as the ';' thats more ignorance than deliberate practice. I only started studying Javascript a few days before posting. So, again many thanks for all the help and putting up with silly mistakes :)