0

I want to write a loop that will display numbers from 1-100 in multiples that the user picks.

Here's my JS code.

 var x = prompt("Enter the increment you want to see")

   for (i=0;i<=100;i=i+x) {
    document.write(i+"</br>")
   }

For example, if I enter "10" I want the code to print the numbers 10, 20,30,40,50,60,70,80,90,100

Why does this not work?

I am teaching myself Javascript, and I am going crazy trying to figure this out.

Can anyone help?

1
  • 1
    Don't use document.write. Commented Mar 17, 2015 at 18:41

2 Answers 2

3

The returned value from prompt is string. You need to parse it as a number (with the radix value as stated in the comments):

var x = parseInt(prompt("Enter the increment you want to see"), 10);
Sign up to request clarification or add additional context in comments.

2 Comments

Worth noting that it's still recommended to use the radix parameter when using parseInt, at least for the time being.
Thank you so much! I'll never forget that prompt returns a string value.
1

You need to parse x. The following code should work -

var x = parseInt(prompt("Enter the increment you want to see"));

for (i = 0; i <= 100; i = i + x) {
  document.write(i + " </br>");
}

1 Comment

Probably more efficient to parse it once at the beginning instead of each iteration of the loop.

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.