-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
72 lines (62 loc) · 2.83 KB
/
main.js
File metadata and controls
72 lines (62 loc) · 2.83 KB
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
// Declaring the input fields
let mortgage = document.getElementById("mortgage");
let term = document.getElementById("term");
let interest = document.getElementById("interest");
let clear = document.getElementById("clear");
let calculate = document.getElementById("calculate");
let firstAppend = document.getElementById("content");
let hidden = document.getElementById("hidden");
let anotherstyle = document.getElementById("anotherstyle");
let showing = document.getElementById("second-writtenaspect");
// Clear button functionality
clear.addEventListener("click", function() {
mortgage.value = "";
term.value = "";
interest.value = "";
let checkedRadio = document.querySelector('input[name="mortgage-type"]:checked');
if (checkedRadio) {
checkedRadio.checked = false;
}
hidden.style.display = "block";
firstAppend.innerHTML = "";
anotherstyle.innerHTML = "";
showing.style.display = "none";
});
// Calculate button functionality
calculate.addEventListener("click", function(event) {
event.preventDefault();
let mortgageValue = parseFloat(mortgage.value);
let termValue = parseInt(term.value);
let interestValue = parseFloat(interest.value) / 100;
let mortgageType = document.querySelector('input[name="mortgage-type"]:checked');
if (!mortgageValue || mortgageValue <= 0 || !termValue || termValue <= 0 || !interestValue || interestValue <= 0 || !mortgageType) {
alert("Please fill out all fields with valid values and select a mortgage type.");
return;
}
mortgageType = mortgageType.value;
let monthlyInterestRate = interestValue / 12;
let numberOfPayments = termValue * 12;
// Clear previous results
firstAppend.innerHTML = "";
anotherstyle.innerHTML = "";
let create = document.createElement("h3");
create.style.color = "gold";
create.style.marginBottom = "20px";
firstAppend.appendChild(create);
let creates = document.createElement("h1");
creates.style.color = "white";
creates.style.marginBottom = "20px";
anotherstyle.appendChild(creates);
hidden.style.display = "none";
showing.style.display = "block";
if (mortgageType === "repayment") {
let monthlyPayment = mortgageValue * (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numberOfPayments)) /
(Math.pow(1 + monthlyInterestRate, numberOfPayments) - 1);
create.textContent = `Monthly Payment: $${monthlyPayment.toFixed(2)}`;
creates.textContent = `Total Repayment: $${(monthlyPayment * numberOfPayments).toFixed(2)}`;
} else if (mortgageType === "interests") {
let monthlyPayment = mortgageValue * monthlyInterestRate;
create.textContent = `Monthly Payment: $${monthlyPayment.toFixed(2)}`;
creates.textContent = `Total Interest: $${(monthlyPayment * numberOfPayments).toFixed(2)}`;
}
});