Skip to content

Instantly share code, notes, and snippets.

@johhansantana
Last active June 14, 2024 00:08
Show Gist options
  • Save johhansantana/ef2d04b8d80e8db3d47972b00ae3314f to your computer and use it in GitHub Desktop.
Save johhansantana/ef2d04b8d80e8db3d47972b00ae3314f to your computer and use it in GitHub Desktop.
mortgage calculator

To help you create a JavaScript program that calculates mortgage payments and total interest paid, we'll follow the formulas given in the image:

  1. Monthly payment formula: [ \text{payment} = p \times r / \left(1 - (1 + r)^{-n}\right) ] where:

    • ( p ) is the amount of the loan
    • ( r ) is the monthly interest rate (annual rate divided by 12)
    • ( n ) is the duration of the loan in months
  2. Total interest formula: [ \text{total interest} = n \times \text{payment} - p ]

Below is the JavaScript code to implement this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mortgage Analyzer</title>
    <script>
        function analyzeMortgage() {
            // Get input values
            let loanAmount = parseFloat(document.getElementById('loanAmount').value);
            let annualRate = parseFloat(document.getElementById('annualRate').value);
            let loanDuration = parseInt(document.getElementById('loanDuration').value);

            // Validate inputs
            if (isNaN(loanAmount) || isNaN(annualRate) || isNaN(loanDuration) || loanAmount <= 0 || annualRate <= 0 || loanDuration <= 0) {
                alert('Please enter valid positive numbers for all fields.');
                return;
            }

            // Calculate monthly interest rate
            let monthlyRate = annualRate / 100 / 12;

            // Calculate monthly payment
            let monthlyPayment = (loanAmount * monthlyRate) / (1 - Math.pow(1 + monthlyRate, -loanDuration));

            // Calculate total interest paid
            let totalInterest = (loanDuration * monthlyPayment) - loanAmount;

            // Display results
            document.getElementById('monthlyPayment').innerText = monthlyPayment.toFixed(2);
            document.getElementById('totalInterest').innerText = totalInterest.toFixed(2);
        }
    </script>
</head>
<body>
    <h1>Mortgage Analyzer</h1>
    <label for="loanAmount">Amount of loan:</label>
    <input type="number" id="loanAmount" required><br><br>
    <label for="annualRate">Interest rate (such as 5.25):</label>
    <input type="number" step="0.01" id="annualRate" required><br><br>
    <label for="loanDuration">Duration in months:</label>
    <input type="number" id="loanDuration" required><br><br>
    <button onclick="analyzeMortgage()">Analyze</button>

    <h2>Results</h2>
    <p>Monthly payment: $<span id="monthlyPayment"></span></p>
    <p>Total interest paid: $<span id="totalInterest"></span></p>
</body>
</html>

This code creates a simple web page with fields for the loan amount, interest rate, and loan duration. When the "Analyze" button is clicked, it calculates the monthly payment and total interest paid, displaying the results on the page.

To use this code:

  1. Copy and paste it into an HTML file.
  2. Open the HTML file in a web browser.
  3. Enter the loan amount, interest rate, and loan duration.
  4. Click the "Analyze" button to see the results.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment