I have typed the following below. I am attempting to run a while loop to allow the user to input positive numbers only and display them on the three input boxes located on myForm. I have added the following reset to the form so it would clear the form: <input type="reset" onclick="myForm" value="Reset the form" ></input>. It clears the form but when I click the input box to add new variables the old ones reappear and it doesn't prompt for new inputs. How do I correct the code to fix this issue? I have added the code below with comments to help show what I was trying to create.
first = -1;
second = -1;
third = -1;
function getValues() {
//while loop to not allow negative numbers or strings//
while (isNaN(first) || first == "" || first < 0) {
first = prompt("Enter the first number.");
break;
}
//while loop to not allow negative numbers or strings//
while (isNaN(second) || second == "" || second < 0) {
second = prompt("Enter the second number.");
break;
}
//while loop to not allow negative numbers or strings//
while (isNaN(third) || third == "" || third < 0) {
third = prompt("Enter the third number.");
break;
}
document.myForm.first.value = first;
document.myForm.second.value = second;
document.myForm.third.value = third;
}
function determineLarge() {
let message = "";
//if statement to determine first number is the largest//
if (second < first && third < first) {
message = "The first number" + "(" + first + ")" + " is larger.";
}
// else if statement to determine second number is the largest//
else if (first < second && third < second) {
message = "The second number" + "(" + second + ")" + " is larger.";
}
//else if statement to determine the third number is the largest//
else if (first < third && second < third) {
message = "The third number " + "(" + third + ")" + " is larger.";
}
//else if statement to determine the first number as the largest number if any values are equal//
else if (first == second || second == first || first == third || second == third) {
message = "The first number" + "(" + first + ")" + " is largerer.";
}
document.getElementById("results").innerHTML = message;
//fucntion to reset form//
function myForm() {
document.getElementById("myForm").reset();
}
}
<body>
<h1>Gary's Largest of Three Numbers</h1>
<!-- estasblishing form name-->
<form name="myForm">
<p> Enter the first number</p>
<!--prompts user for first input-->
<input type="number" name="first" value="" onclick="javascript:getValues();">
<p>Enter the second number</p>
<!--prompts user for second input-->
<input type="number" name="second" value="" onclick="javascript:getValues();">
<p>Enter the third number</p>
<!--prompts user for third input-->
<input type="number" name="third" value="" onclick="javascript:getValues()">
<!--runs the function to determine the largest number-->
<button type="button" onclick="determineLarge();">Determine the larger number</button>
<!--resets the form so user can insert different numbers-->
<input type="reset" onclick="myForm" value="Reset the form"></input>
</form>
<div id="results"> </div>