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 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2005
    Location
    England
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Arithmatic with variables

    This is probably a really easy question, but how can I involve variables (with numeric values) in arithmatic with other numeric-valued variables and actual numbers? Cos in a script I'm writing I try to do that and it doesn't work. It says the result is NaN (not a number).
    Code:
    var x = variable1*16;
    var y = variable2 + variable3
    Does someone know something I don't?
    Congratulations on discovering the hidden sig! Click here to claim your prize!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Make sure that variable1, variable2 and variable3 are numbers and not strings.

    You can ensure this by variable1=variable1*1

  • #3
    New Coder
    Join Date
    Jan 2005
    Location
    England
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by AlterEgo
    how can I involve variables (with numeric values)
    They are
    Congratulations on discovering the hidden sig! Click here to claim your prize!

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Are you really sure they are?

    I am sure you would run


    Code:
    alert(isNaN(variable1));
    alert(isNaN(variable2));
    alert(isNaN(variable3));
    at least one would say true.
    If the content in the variable is a number, that does not mean it is a number. That might not make sense, but there is a difference between a string and a integer/float. what you should do is

    Code:
    var x = parseFloat(variable1)*16;
    var y = parseFloat(variable2) + parseFloat(variable3);
    Also if you are pulling values from form fields, they are strings.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

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