I'm starting to learn javascript and I have the first problem. In the task I want to perform a loop that prints the numbers from the user-specified range, example: for values: 2 and 10, prints in the div: 2,3,4,5,6,7,8,9,10. My code:
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;
}
below is the html code:
<!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>
Thanks for the help, MD.
++ is the increment operator.. for ex i++ means i=i+1 for(int i=0;i<10;i++) { System. out. printline(i); } In the following example first of all the intial value of i is 0 so 0<10 it comes inside the loop and print the value of i again the value of i is incremented to 1(i=i+1)
JavaScript supports different kinds of loops: for - loops through a block of code a number of times. for/in - loops through the properties of an object. for/of - loops through the values of an iterable object.
In programming, loops are used to repeat a block of code. For example, if you want to show a message 100 times, then you can use a loop. It's just a simple example; you can achieve much more with loops.
You have to pass the object you want to iterate, and the JavaScript Object. keys() method will return an array comprising all keys or property names. Then, you can iterate through that array and fetch the value of each property utilizing an array looping method such as the JavaScript forEach() loop.
You need to convert numberA
and numberB
to Numbers first
var numberA = +document.getElementById("numberA").value, //observe unary +
numberB = +document.getElementById("numberB").value, //observe unary +
Demo
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;
}
<input type="text" id="numberA" />
<input type="text" id="numberB" />
<input type="submit" value="Show" onclick="loopChecking()" />
<div id="numbers"></div>
Your numberA
and numberB
are not seen as number. You need to parse them
var numberA = parseInt(document.getElementById("numberA").value,10),
numberB = parseInt(document.getElementById("numberB").value,10),
First of all you have to chech if your numbers are actually numbers.
Also you could add check for (numberA < numberB
) or specify the loop according to numberA and numberB values.
function loopChecking() {
var numberA = Number(document.getElementById("numberA").value),
numberB = Number(document.getElementById("numberB").value),
result = "";
if (numberA && numberB) {
for (var i = numberA; i <= numberB; i++) {
result += i + ' ';
}
}
document.getElementById("numbers").innerText = result;
}
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With