I want to perform a loop that prints the numbers from the range specified by the user, for example:
- for values: 2 and 10, it prints in the div: 2,3,4,5,6,7,8,9, 10.
function loopChecking() {
'use strict';
var numberA = document.getElementById("numberA").value,
numberB = document.getElementById("numberB").value,
i = 0,
result = "";
for (i = numberA; i <= numberB; i += 1) {
result += i + " ";
}
document.getElementById("numbers").innerHTML = result;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Loop</title>
<script src="numbers.js"></script>
</head>
<body>
<input type="text" id="numberA" />
<input type="text" id="numberB" />
<input type="submit" value="Show" onclick="loopChecking()" />
<div id="numbers"></div>
</body>
</html>
P.S:
- font
- I recommend not to run, it loops.
The
value
delinput
is going to be a value of typeString
, you must convert it to numeric values to be able to operate with them as numbers.Look at this example.
I use the method
parseInt
to convert the values of theinput
to integers.I then check that the result of both was a correct numeric value before continuing to use the function
isNaN
.