I am struggling to set up my calculator so that it triggers an error message if the APR goes over 25% or falls below 0. Also, the Loan Term should be greater than 0 and less than or equal to 40, otherwise display an error message. I have attempted different approaches but haven't been able to solve it.
APR (Annual Percentage Rate)—this is a text input field. Assign the name and id of apr. It should only accept floating point values between 0 and 25.00%. Loan Term (in years)—another text input field with the name and id term. The value must be greater than zero and less than or equal to 40. Loan Amount (in dollars)—a text input field with the name and id amount.
<div>
<form action="">
<label> APR%: </label><br>
<label><input type="number" id="apr" name="APR" placeholder ="Enter Annual Percentage Rate...."/></label><br>
<label> Loan Term: </label><br>
<label> <input type="number" id="term" name="Term"placeholder ="Enter Loan Term...."/></label><br>
<label> Loan Amount: </label><br>
<label> <input type="number" id="amount" name="Amount" placeholder ="Enter Loan Amount...."/></label><br>
<label> Monthly Payment: </label><br>
<input type="text" id="payment" name="mPmt" placeholder ="Display Monthly Payment"/><br>
<input type="button" onclick = "errors()" id="sbt" value="Submit" />
<input type="reset" id="rst" value="Reset Form" />
</form>
</div>
<script>
var term;
var apr;
var amount;
var mPmt;
window.onload = function()
{
document.getElementById("apr").focus();
document.getElementById("sbt").onclick = getValues;
};
//use toFixed(2) to set the precision of the mPayment. Use it on an int.
function getValues()
{
term = document.getElementById("term").value;
apr = document.getElementById("apr").value;
amount = document.getElementById("amount").value;
apr /=1200;
term *= 12;
mPmt = calculatePayment();
document.getElementById("payment").value = "$" + mPmt.toFixed(2);
};
function calculatePayment()
{
var payment = amount*(apr * Math.pow((1 + apr), term))/(Math.pow((1 + apr), term) - 1);
return payment;``
}
function errors() {
var message , x;
message = document.getElementById("01");
message.innerHTML ="";
x = document.getElementById("apr").value;
try{
if (x == "") throw "empty";
if (isNaN(apr)) throw "not a number";
x = Number(x);
if (apr < 0) throw "should be a postive integer";
if (apr > 25) throw "apr can only be up to 25.00% but not less than 0";
}
catch(err) {
message.innerHTML = "Input is " + err
}
}
</script>