-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugJavaScript.html
More file actions
54 lines (48 loc) · 1.91 KB
/
DebugJavaScript.html
File metadata and controls
54 lines (48 loc) · 1.91 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mortgage Calculator</title>
</head>
<body>
<h1>Mortgage Calculator</h1>
<form id="mortgageForm">
<table>
<tr>
<td><label for="principal">Loan Amount ($):</label></td>
<td><input type="number" id="principal" required></td>
</tr>
<tr>
<td><label for="rate">Interest Rate (%):</label></td>
<td><input type="number" id="rate" step="0.01" required></td>
</tr>
<tr>
<td><label for="years">Loan Term (years):</label></td>
<td><input type="number" id="years" required></td>
</tr>
</table>
<button type="button" onclick="calculateMortgage()">Calculate</button>
</form>
<p id="result"></p>
<script>
function calculateMortgage() {
// Get the initial amount from the principal box
const principal = document.getElementById("principle").value;
// update rate from the box
const rate = Number(document.getElementById('rate').value) * 0.01;
rate = document.getElementById("rate").value / 100;
// get the years to calculate
const years = document.getElementById("years).value;
// Call the power funtion, pow, from Math
const monthlyRate = Math.pw(1 + rate, years * 12);
if (rate === 0) {
document.getElementById("result").innerText = "Interest rate cannot be zero.";
return;
}
const monthlyPayment = (principal * rate * monthlyRate) / (monthlyRate - 1);
document.getElementById("result").innerText = `Your monthly payment is $${monthlyPayment.toFixed(2)}`;
}
</script>
</body>
</html>