Loan Shark Code

                        
                            function getValues(){
                                let loan = document.getElementById("amount").value;
                                let payments = document.getElementById("payments").value;
                                let rate = document.getElementById("rate").value;
                                mPayments = monthlyPayments(payments,loan, rate);
                                displayResults(payments, loan, rate, mPayments)
                            }
                            function monthlyPayments(payment,loan,rate){
                                let monthlyPayment = (loan)*(rate/1200)/(1- Math.pow((1+ rate/1200), -payment));
                                monthlyPayment = monthlyPayment.toFixed(2);
                                return monthlyPayment;
                            }
                            function displayResults(months, loan, rate, mPayments){
                                let rUpdate = "";
                                let tInterest = 0;
                                let originalLoan = parseInt(loan);
                                let pDisplay = "$"+mPayments;
                                document.getElementById("totalPrincipalDisplay").innerHTML = "$" + originalLoan;
                                for(term = 1; term<= months; term++){
                                    let interest = (loan*rate/1200);
                                    let principal = (mPayments - interest);
                                    tInterest += interest;     
                                    loan -= principal;
                                    interest = Math.round((interest + Number.EPSILON)*100)/100;
                                    principal = Math.round((principal + Number.EPSILON)*100)/100;
                                    loan = Math.round((loan + Number.EPSILON)*100)/100;
                                    tInterest = Math.round((tInterest + Number.EPSILON)*100)/100;
                                    rUpdate += `${term}${mPayments}${principal}${interest}${tInterest}${loan}`;
                                }
                                
                                tCost = originalLoan + tInterest;
                                document.getElementById("results").innerHTML = rUpdate;
                                document.getElementById("paymentDisplay").innerHTML = pDisplay;
                                document.getElementById("totalInterestDisplay").innerHTML = "$" + Math.round((tInterest + Number.EPSILON)*100)/100;
                                document.getElementById("totalCostDisplay").innerHTML = "$" + tCost.toFixed(2);
                            }
                        
                    
Code Structure
getValues()

This function gets the loan amount, number of payments, and rate of interest from the user inputs. It then calculates the monthly payments by calling monthlyPayments() and calls displayResults().

monthlyPayments()

This function multiplies the loan by the rate divided by 1200. Then it divides this amount and rounds the amount within two decimal places to obtain the monthly payments and returns the monthly payment amount to the getValues() function.

displayResults()

This function parses the loan amount into an integer. It then displays the principal payment to the website. It runs a for loop from 1 to the amount of payments, and calculates the interest each time by multiplying loan by rate and dividing by 1200. It then subtracts the monthly payments by the interest and adds the interest amount to the variable tInterest. It deducts the principal from the loan and rounds the numbers. It then adds the information in HTML formatting to the rUpdate string. It adds the original loan amount and the interest to represent the total cost. Afterwards, it displays the payments, total interest and total cost to the user.