Jump to content

Interprets as string instead of number


Wilshire

Recommended Posts

I'm trying to teach myself javascript, and going through some totorials I saw a counting example where it would count from 0 to 9 and display it. It was a little too easy, so I made my own version which would ask you where to start counting, where to end, and what to count by. For some reason, it combines everything into a string instead of adding the numbers. Code is below, any help would be appreciated.

<html><head></head><body><script type="text/javascript">var start=prompt("Start at:");var end=prompt("End at:");var increment=prompt("Count by:");while (start<=end){document.write(start+"<br />");start+=increment;}</script></body></html>

Link to comment
Share on other sites

You could set the variables to the datatype you want :) Like this way:

var start=Number(prompt("Start at:"));
This is because the datatype the prompt function returns, is always a string. If you want it to act as a digit, number, use this function :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...