I'm currently working on a basic JavaScript Mortgage calculator, but I'm facing some challenges. I haven't implemented the math yet and I'm struggling to display the final sum (x + y + z) below the form after submission. The reset form button also doesn't seem to be working. Can anyone offer assistance? Thank you!
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- This is assign05.html -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> JavaScript Mortgage Calculator </title>
<script>
// Assign values to variables
function myFunction() {
var x = parseInt(document.getElementById("apr").value);
var y = parseInt(document.getElementById("loanTerm").value);
var z = parseInt(document.getElementById("loanAmount").value);
}
//Display the total sum
function display(x, y, z) {
var num = x + y + z;
var n = num.toString();
document.getElementById("total").innerHTML = n;
}
// Validate the form
function validateForm(x, y, z) {
var x = document.forms["myForm"]["apr"].value;
var y = document.forms["myForm"]["loanTerm"].value;
var z = document.forms["myForm"]["loanAmount"].value;
// If statements
if (x==null || x=="") {
alert("APR must be filled out");
document.getElementById("apr").focus();
return false;
}
else if (y==null || y=="") {
alert("Loan Term must be filled out");
document.getElementById("loanTerm").focus()
return false;
}
else if (z==null || z=="") {
alert("Loan Amount must be filled out");
document.getElementById("loanAmount").focus()
return false;
}
else {
// Call and display the sum. (This isn't working)
document.getElementById("demo").innerHTML = display(x, y, z);
}
//Reset the form (this isn't working)
function resetForm() {
document.getElementById("myForm").reset();
}
</script>
</head>
<body onLoad=document.getElementById("apr").focus();>
<form name="myForm" action="" onsubmit="return validateForm()" method="post">
APR: <input type="number" id="apr" value=""><br/>
Loan Term: <input type="number" id="loanTerm" value=""><br/>
Loan Amount: <input type="number" id="loanAmount" value=""><br/>
<button onclick="myFunction()">Calculate Payment Button</button>
<input type="button" onclick="resetForm()" value="Reset form">
</form>
</body>
</html>