0

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

3
  • Replace document.write with insertAdjacentHTML(). Commented Sep 19, 2018 at 10:09
  • Then input 0 value then you 0to9 as result Commented Sep 19, 2018 at 10:14
  • Just add: a = +a;. The unary operator (+) will cast the variable a to a number, since prompts actually returns strings, so using + on strings will result in concatenating. Commented Sep 19, 2018 at 10:32

8 Answers 8

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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++;
      }
};

Comments

1

Try it with a for-loop:

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

1 Comment

Sorry my question was wrong. Not from 0, but from the number I choose at prompt.
1

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

Comments

0

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

Comments

0

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.'));

Comments

0

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

Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.