The Code for LOAN SHARK.


                

The Code is structured in three main functions, plus three helper functions.

getValues()

The getValues() function is responsible for retrieving the numbers the user provides in the input fields, parsing and validating them. If all inputs provided by the user are numbers then it will create the starting loan object and call the getPayments() and displayResults() functions described below.


getPayments()

The getPayments() function takes one parameter: the loan object with the data. In this function we iterate over the duration of the loan term, and for each month all the required values are calculated. Every month data is stored in an array that will be used later to display the results. Finally the total values are tallied and the data is passed as the return value;

displayResults()

The displayResults() function takes one parameter: the loan object with the data. The HTML table is retrieved and emptied every time new data is parsed. A template was used to facilitate the reuse and construction of the table rows (see HTML page). All the information regarding a single month payment is added to a row.


Helper functions.

calcPayment()

The calcPayment() function takes three parameters: the loan amount, rate and term. It then calculates the user monthly payment for the duration of the loan.


calcMonthlyRate()

The calcMonthlyRate() function takes one parameter: the loan interest rate. This rate correspond to the yearly interest, which is converted to a monthly interest rate to be used in further calculations.


calcMonthlyInterest()

The calcMonthlyInterest() function takes two parameters: the current balance and the monthly interest rate. This is used to calculate how much is paid in interest each month.