Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loop in javascript [from X to Y]

Tags:

javascript

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.

like image 805
Mate Usz Avatar asked Nov 28 '17 10:11

Mate Usz


People also ask

What does i ++ mean in JavaScript for loop?

++ 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)

Can you loop in JavaScript?

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.

What is loop in JavaScript with example?

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.

How do you iterate keys in JavaScript?

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.


3 Answers

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>
like image 142
gurvinder372 Avatar answered Oct 16 '22 23:10

gurvinder372


Your numberAand 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),
like image 45
Stéphane Ammar Avatar answered Oct 16 '22 23:10

Stéphane Ammar


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>
like image 1
Mikhail Katrin Avatar answered Oct 16 '22 22:10

Mikhail Katrin