MARA
MARA

Reputation: 3

while loop doesn't stop in js

I want to use function with while to print only 10 numbers starting from the number I choose.

But while loop doesn't stop looping.

id = prompt('Write any number.');
function numbering(a) {
    var i = a; 
    var j = a + 10;  
    while (i < j) {
       document.write(i);
       i++;
    }
};
numbering(id);

Upvotes: 0

Views: 1995

Answers (8)

Aniket Sinha
Aniket Sinha

Reputation: 6031

When you use function numbering(a) {, the variable a is passed as string.

This results in i and j being set as string.

Taking example: Suppose you pass 2 as input, your variables will be set as a="2", i="2" and j="210". So according to your condition, it'll print starting from 2 till 209.

You can change your code to parse a as number to achieve your result; something like:

function numbering(a) {
    a = parseInt(a);  //parse as Int here
    var i=a; var j=a+10;
    while (i < j) 
      {
         document.write(i);
         i++;
      }
};

Upvotes: 2

Mohamed Sadat
Mohamed Sadat

Reputation: 255

You can try with this for-loop function :

 id=prompt('Write any number.');
 function numbering(a) {
  for (let i = a; i < a + 10; i++){
    document.write(i);
  }
 };

numbering(id);

You can add a parseInt(id) to you numbering function parameter call if you want to parse the input into a number

Upvotes: 1

Suresh
Suresh

Reputation: 923

document.write(i +"<br>");

In console i am getting proper result. just add line break to see whole result in viewport

Upvotes: 0

Houssein Zouari
Houssein Zouari

Reputation: 722

you need the parseInt function because what you getting from promt is string. For example 5 +10 . For javascript , it will be 510 .

     id=prompt('Write any number.');
function numbering(a) {
  var i=parseInt(a); var j=parseInt(a)+10; 
  while (i < j){
    document.write(i);i++;
  }};

             numbering(id);

Upvotes: 0

bruno.almeida
bruno.almeida

Reputation: 2896

The prompt function returns a string. The problem is that you are concatenating a string with the number 10.

If you write your variables to document, you can see the problem:

     id=prompt('Write any number.');
    function numbering(a) {
        var i=a; var j=a+10; 
        document.write(i);
        document.write("<br/>");
        document.write(j);
        document.write("<br/>");
        //while (i < j){
        //  document.write(i);
        //  i++;
        //}
    };

         numbering(id);

This will fix your problem:

var id=parseInt(prompt('Write any number.'));

Upvotes: 0

EndoM8rix
EndoM8rix

Reputation: 66

When you enter a number in the prompt, it is supplied as a string. Since you have not converted this into a number, the line var j = a + 10 is actually joining the two values as if they were strings.

For example, if you enter "5" into the prompt, then var j = "5" + 10 returns "510". Only when you then compare the two variables in the while loop with i < j does it get interpreted as a number, and will loop from 5 to 510.

The easiest way to convert a string into a number is to use parseInt(value).

Upvotes: 0

Stefan Orzu
Stefan Orzu

Reputation: 433

Try this:

var id = parseInt(prompt('Write any number.'), 10);

In your example id will be of type string and the comparisons won't work as you expect.

Upvotes: 4

user10182922
user10182922

Reputation:

Try it with a for-loop:

for(var i=0; i<10; i++){ doSomething}

Upvotes: 1

Related Questions