-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
72 lines (60 loc) · 2.83 KB
/
main.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
document.addEventListener('DOMContentLoaded', function() {
const carPriceInput = document.getElementById('carPrice');
const carPriceValue = document.getElementById('carPriceValue');
const downPayment = document.getElementById('downPayment');
const repaymentTerm = document.getElementById('repaymentTerm');
const repaymentTermDisplay = document.getElementById('repaymentTermDisplay');
const incomeAmount = document.getElementById('incomeAmount');
const incomeFrequency = document.getElementById('incomeFrequency');
const monthlyIncome = document.getElementById('monthlyIncome');
const calculateBtn = document.getElementById('calculateBtn');
const car = document.getElementById('car');
function updateCarPrice() {
const carPrice = parseFloat(carPriceInput.value) || 0;
carPriceValue.textContent = `$${carPrice.toLocaleString()}`;
const downPaymentValue = carPrice * 0.2;
downPayment.textContent = `$${downPaymentValue.toLocaleString()}`;
}
function updateRepaymentTerm() {
repaymentTermDisplay.textContent = repaymentTerm.value;
}
function updateIncome() {
const income = parseFloat(incomeAmount.value) || 0;
const frequency = incomeFrequency.value;
let monthlyIncomeValue = income;
if (frequency === 'yearly') {
monthlyIncomeValue = income / 12;
}
monthlyIncome.textContent = `$${(monthlyIncomeValue * 0.1).toFixed(2)}`;
}
carPriceInput.addEventListener('input', updateCarPrice);
repaymentTerm.addEventListener('change', updateRepaymentTerm);
incomeAmount.addEventListener('input', updateIncome);
incomeFrequency.addEventListener('change', updateIncome);
updateCarPrice();
updateRepaymentTerm();
updateIncome();
document.addEventListener('mousemove', (event) => {
const carWidth = car.offsetWidth;
const carHeight = car.offsetHeight;
const centerX = event.clientX - carWidth / 2;
const centerY = event.clientY - carHeight / 2;
car.style.left = `${centerX}px`;
car.style.top = `${centerY}px`;
car.style.display = 'block'; // Ensure car is visible when moving
});
calculateBtn.addEventListener('click', () => {
const carPrice = carPriceInput.value;
const downPaymentValue = carPrice * 0.2;
const termYears = repaymentTerm.value;
const income = incomeAmount.value;
const frequency = incomeFrequency.value;
let monthlyIncomeValue = income;
if (frequency === 'yearly') {
monthlyIncomeValue = income / 12;
}
monthlyIncomeValue *= 0.1;
const queryString = `carPrice=${carPrice}&downPayment=${downPaymentValue}&termYears=${termYears}&monthlyIncome=${monthlyIncomeValue}`;
window.location.href = `results.html?${queryString}`;
});
});