why are negative integers not registering in my text box?

If test1 is less than zero, the loop will never run, so the negative check won’t happen.

Do the check before the loop.

Try this code:

<script>
function EnterNumber() {
var test1 = document.getElementById("t1").value;
var text = "";
var i;

if (test1 < 0){
    text = "No valid number was entered, no negatives can be submitted."
    //document.write("No valid number was entered, no negatives can be submitted.");
  }

for (i = 0; i <= test1; i++) {
  text += i + "<br>";
}

document.getElementById("result").innerHTML = text;
}
</script>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top