The Code
The code is structured in four functions
getUserInput()
This function is what allows the user to interact with the calculator. The function waits and listen for the user to click the "Submit" button. Once the button is pressed, it takes in the information provided by the user on the App page, and first verifies that the information provided are numbers.
After the verification is completed, if the information does not pass the check, it will prompt the user to enter numbers only. If the information does pass the check, it stores it for use in other parts of the program to use.
calculateAmounts()
This function receives the information stored in the getUserInput()
function to calculate the various parts of the mortgage payment. This function will continuously calculate the information to generate the breakdown for each months payment.
The function will never generate more information than needed because it uses the term amount to set it's limit. Once it's done breaking down the payment information for each month, it will store each piece of information with a label that it can pass on to another function to use.
displayMortgageData()
This function takes in the labeled information from the calculateAmounts()
function as well as the initial requested loan amount. It then takes that information places it into a template to allow the breakdown of each monthly payment to be displayed on a table on the page. This function also only works until it reaches the maximum amount of months defined by the term.
formatCurrency()
This function converts all the number values in the displayMortgageData()
function into US Currency values instead of very long decimal points.