Mortgage Calculator
/* General reset */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Body styling */
body {
font-family: 'Arial', sans-serif;
background: linear-gradient(135deg, #ff7e5f, #feb47b);
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
/* Container styling */
.container {
width: 100%;
max-width: 450px;
padding: 20px;
background: white;
border-radius: 10px;
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
}
/* Heading styling */
h1 {
text-align: center;
margin-bottom: 30px;
color: #333;
}
/* Input field styling */
.input-field {
margin-bottom: 15px;
}
label {
font-size: 16px;
color: #333;
display: block;
margin-bottom: 5px;
}
input {
width: 100%;
padding: 10px;
font-size: 16px;
border-radius: 5px;
border: 1px solid #ccc;
outline: none;
}
input:focus {
border-color: #ff7e5f;
}
/* Button styling */
button {
width: 100%;
padding: 12px;
font-size: 18px;
background-color: #ff7e5f;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
margin-top: 20px;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #feb47b;
}
/* Result section styling */
#result {
margin-top: 30px;
background-color: #f7f7f7;
padding: 15px;
border-radius: 8px;
text-align: center;
display: none;
}
#result p {
font-size: 18px;
margin: 10px 0;
}
#result span {
font-weight: bold;
color: #ff7e5f;
}
/* Media Query for responsiveness */
@media (max-width: 600px) {
.container {
padding: 15px;
}
h1 {
font-size: 24px;
}
input, button {
font-size: 14px;
}
}
function calculateMortgage() {
// Get input values
const loanAmount = parseFloat(document.getElementById('loanAmount').value);
const interestRate = parseFloat(document.getElementById('interestRate').value) / 100 / 12; // Monthly interest rate
const loanTerm = parseInt(document.getElementById('loanTerm').value) * 12; // Monthly term in months
// Validate inputs
if (isNaN(loanAmount) || isNaN(interestRate) || isNaN(loanTerm) || loanAmount <= 0 || interestRate <= 0 || loanTerm <= 0) {
alert('Please enter valid values for all fields.');
return;
}
// Calculate mortgage
const x = Math.pow(1 + interestRate, loanTerm);
const monthlyPayment = (loanAmount * interestRate * x) / (x - 1);
if (!isNaN(monthlyPayment) && (monthlyPayment !== Infinity) && (monthlyPayment !== -Infinity) && (monthlyPayment > 0)) {
const totalPayment = monthlyPayment * loanTerm;
const totalInterest = totalPayment - loanAmount;
// Display results
document.getElementById('monthlyPayment').textContent = `$${monthlyPayment.toFixed(2)}`;
document.getElementById('totalPayment').textContent = `$${totalPayment.toFixed(2)}`;
document.getElementById('totalInterest').textContent = `$${totalInterest.toFixed(2)}`;
// Show result section
document.getElementById('result').style.display = 'block';
} else {
alert('Error in calculation. Please check your input values.');
}
}