Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2006
    Location
    Toronto
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Addition Operator

    In most of my scripts concerning math operators, I use prompts. The bad thing about prompt variables is that, although you can subtract, multiply, and divide them normally, addition treats the variables as strings, and does not add the values. This has proven to be an annoying problem, and although I have found a solution through loops and an if statement, suffice to say it is inconvenient, and does not work with decimals. I noticed that when I declared the variables myself, they are added as numbers not strings, because the variable is a number. Is there a way to make a variable declared through prompt a definite number so addition does not add strings?

    PS: By prompt I mean

    var a = prompt("Type in a number here", "")
    var currentTime = new Date()
    var chance = currentTime.getTime()
    var chance2 = chance % 3
    alert(chance2)

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Code:
    var a = Number(prompt("Type in a number here", ""));

  • Users who have thanked glenngv for this post:

    1337coder (08-26-2007)

  • #3
    New Coder
    Join Date
    Sep 2006
    Location
    Toronto
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks a lot, this saves a lot of time.
    var currentTime = new Date()
    var chance = currentTime.getTime()
    var chance2 = chance % 3
    alert(chance2)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •