The Code for Loan Shark.


                    
                    //Get info from the user. Calculate Results

                    let cPayments = parseFloat(months.value);
                    function getValues(){
                    
                        console.log('Calculating...');
                        // Form variables
                        let amount = document.getElementById('amount');
                        let interest = document.getElementById('interest');
                        let months = document.getElementById('months');
                        
                        // parse the numbers
                        let principal = parseFloat(amount.value);
                        let cInterest = parseFloat(interest.value) / 100/ 12;
                        let cPayments = parseFloat(months.value);
                       
                        //Compute monthly payment with calculate function - Casey Spaulding
                            calculate(principal, cPayments, cInterest)
                        }
                    
                    
                    function calculate(principal, cPayments, cInterest){
                    
                        //Compute monthly payment, interest, etc 
                        let x = Math.pow(1+cInterest, cPayments);
                        let monthly = (principal * x * cInterest)/(x-1);
                        let total_payments = (monthly * cPayments).toFixed(2);
                        let total_interest_sum = ((monthly * cPayments)-principal).toFixed(2);
                    
                        let formatCurrency = new Intl.NumberFormat(undefined, {
                            style: 'currency',
                            currency: 'USD'
                        });
                        let fmonthly = formatCurrency.format(monthly);
                        let fprincipal = formatCurrency.format(principal);
                        let ftotal_payments = formatCurrency.format(total_payments);
                        let ftotal_interest_sum = formatCurrency.format(total_interest_sum);
                        
                    
                        let info="";
                    
                        info += "";
                    
                        info += "";
                        info += "";
                    
                        info += "";
                        info += "";
                    
                        info += "";
                        info += "";
                    
                        info += "";
                        info += "";
                    
                        info += "";
                        info += "";
                    
                        info += "

Monthly Payment:

"+fmonthly+"

Loan Amount:"+fprincipal+"
Total Payments:"+ftotal_payments+"
Total Interest Paid:"+ftotal_interest_sum+"
Payments:"+cPayments+"
"; //info is a string containing all the html table code document.getElementById("loan_info").innerHTML = info; // TABLE DATA let table=""; table += ""; table += ""; table += " "; table += ""; table += ""; table += " "; table += ""; table += ""; table += ""; let current_balance = parseFloat(document.getElementById('amount').value); let interest = parseFloat(document.getElementById('interest').value); let monthly_payment = monthly let payment_counter = 1; let total_interest = parseFloat(cInterest); while (payment_counter <= cPayments) { towards_interest = cInterest * current_balance; towards_balance = monthly_payment - towards_interest; total_interest = total_interest + towards_interest; current_balance = current_balance - towards_balance; let ftowards_interest = formatCurrency.format(towards_interest); let ftowards_balance = formatCurrency.format(towards_balance); let ftotal_interest = formatCurrency.format(total_interest); let fcurrent_balance = formatCurrency.format(current_balance); let fmonthly_payment = formatCurrency.format(monthly_payment); table += ""; table += "" table += "" table += "" table += "" table += "" table += "" table += ""; payment_counter++; } table +="
MonthPaymentPrincipleInterestInterest PaidBalance
"+payment_counter+""+fmonthly_payment+""+ftowards_balance+""+ftowards_interest+""+ftotal_interest+""+fcurrent_balance+"
"; document.getElementById("tablehtml").innerHTML = table; } function round(num, dec){ return (Math.round(num*Math.pow(10,dec))/ Math.pow(10,dec)).toFixed(dec); } // Show Error function showError(error){ // Creat a div const errorDiv = document.createElement('div'); // Get elements const card = document.querySelector('.card'); const heading = document.querySelector('.heading'); // Add class errorDiv.className = 'alert alert-danger'; // Create text node and append to div errorDiv.appendChild(document.createTextNode(error)); // Insert error above heading card.insertBefore(errorDiv, heading); // Clear error after 3 seconds setTimeout(clearError, 3000); }

The Code is structured in two functions.

GetValues Function

GetValues is a function that gathers all the data from the page and validates it. Then sends it to the calculate function

Calculate Function

Calculate is a function that calculates the monthly payments, interest, etc and then displays a summary and an amortization table for the user. I used the formatCurrency.format(total_interest_sum) for the amounts.