• Advertisement
Sign in to follow this  

[web] javascript and turning a string into an integer

This topic is 4550 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Sign in to follow this  

  • Advertisement