Sign in to follow this  

[web] javascript and turning a string into an integer

Recommended Posts

I am making a simple dice server as a first project with javascript. I have the user select the dice size, number of dice to roll, and the amount to modify the roll by. These values are stored in a list box in a form; the form is passed to the number generation function when the user hits a submit button. The problem is javascript's lack of type safety, I guess. When I get these values and try to perform integer arithmetic on them, the code looks like this: var modify_dice_by = form.modify.value; ... var roll = RandomInteger(1, 6) + modify_dice_by; Unfortunately, the above treats "roll" as a string; it sets its value to a random integer from 1-6, and then appends the value of modify_dice_by, rather than adding the value. So if modify_dice_by was equal to "3", and RandomInteger() returned 4, roll would be equal to "43", instead of 7. What can I do to solve this? Thanks!

Share this post

Link to post
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

Sign in to follow this