0
var prompt1 = prompt("Number Here");
var something2 = prompt("Increment Here");
for (var i = 5; i <= prompt1; i += 5) {
    alert(i);
}

http://jsfiddle.net/davidhin/wte7d5k9/

Why is it that when I change i += 5 to i += something2, the code stops working?

1
  • What does your fiddle have to do with your question? You'll need to clarify what you're asking by including more explanation of what you're doing and what you're trying to accomplish in your post. Commented Sep 30, 2014 at 8:00

2 Answers 2

6

Because something2 is String. Use in for loop Number(something2) to convert to Number.

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

Comments

1

the prompt is receive a String value,not int value. Imagine that if someone put it prompt "hello" and "world", what will happen?

so you should do those steps :

1/. parse value into int data type.

2/. Validate data types.

example :

      var promptVal = parseInt(prompt1); // return NaN (not-a-number) if input isn't number.
      if(isNaN(prompVal)) {
         return error;
      }

Now you could run for-loop normally.

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.