-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
81 lines (65 loc) · 3.41 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
document.addEventListener("DOMContentLoaded", function() {
// DOM elements
const carType = document.getElementById("car-type");
const carValue = document.getElementById("car-value");
const carValueRange = document.getElementById("car-value-range");
const leasePeriod = document.getElementById("lease-period");
const downPayment = document.getElementById("down-payment");
const downPaymentRange = document.getElementById("down-payment-range");
// Elements to update with calculated values
const leasingCostElement = document.getElementById("leasing-cost");
const downPaymentValueElement = document.getElementById("down-payment-value");
const monthlyInstallmentElement = document.getElementById("monthly-installment");
const interestRateElement = document.getElementById("interest-rate");
// Function to synchronize the value inputs with their corresponding range inputs
function updateValues() {
carValue.value = carValueRange.value;
downPayment.value = downPaymentRange.value;
}
// Function to calculate leasing details based on user inputs
function calculateLeasing() {
const carValueNum = parseFloat(carValue.value);
const leasePeriodNum = parseInt(leasePeriod.value);
const downPaymentPercentNum = parseInt(downPayment.value);
// Calculate down payment amount
const downPaymentValue = (downPaymentPercentNum / 100) * carValueNum;
// Determine annual interest rate based on car type
const isBrandNew = carType.value === "new";
const annualInterestRate = isBrandNew ? 2.99 : 3.7;
// Calculate monthly interest rate
const monthlyInterestRate = annualInterestRate / 12 / 100;
// Calculate remaining lease value after down payment
const leaseValue = carValueNum - downPaymentValue;
// Calculate monthly installment using formula for annuity
const monthlyInstallment = (leaseValue * monthlyInterestRate) / (1 - Math.pow(1 + monthlyInterestRate, -leasePeriodNum));
// Calculate total leasing cost
const totalLeasingCost = (monthlyInstallment * leasePeriodNum) + downPaymentValue;
// Update UI with calculated values
leasingCostElement.textContent = totalLeasingCost.toFixed(2);
downPaymentValueElement.textContent = downPaymentValue.toFixed(2);
monthlyInstallmentElement.textContent = monthlyInstallment.toFixed(2);
interestRateElement.textContent = annualInterestRate.toFixed(2);
}
// Event listeners to update and calculate when inputs change
carValueRange.addEventListener("input", () => {
updateValues();
calculateLeasing();
});
carValue.addEventListener("input", () => {
carValueRange.value = carValue.value;
calculateLeasing();
});
downPaymentRange.addEventListener("input", () => {
updateValues();
calculateLeasing();
});
downPayment.addEventListener("input", () => {
downPaymentRange.value = downPayment.value;
calculateLeasing();
});
leasePeriod.addEventListener("change", calculateLeasing);
carType.addEventListener("change", calculateLeasing);
// Initial update and calculation when the page loads
updateValues();
calculateLeasing();
});