+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    6

    Basic Javascript question

     
    I want to take three values in from the user and perform various basic mathematical operations on them.

    I am taking input like this:
    var firstnumber = prompt("blah blah...
    var secondnumber = prompt("blah blah...

    and then adding them together like this
    var result = firstnumber+secondnumber

    However this just concatenates the values as if they were strings, e.g. if you entered "7" and "3" instead of getting 10 you get 73!

    I know how to add them if I hard-coded the variables, but I want the user to input them.

  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    I ended up just doing:

    firstnumber = firstnumber*1 to force it as a numerical value. Not sure if that's best practise?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Basic javascript help with if function
    By fefefasesdfs in forum Webmastering and Programming
    Replies: 2
    Last Post: August 8th, 2010, 10:29 AM
  2. basic Javascript question -- GetElementByID
    By Olvie in forum Webmastering and Programming
    Replies: 0
    Last Post: April 16th, 2009, 02:40 PM
  3. Very basic Javascript question
    By flutter in forum Webmastering and Programming
    Replies: 5
    Last Post: February 27th, 2009, 11:36 AM
  4. Beginner Javascript question
    By James Engle in forum Webmastering and Programming
    Replies: 0
    Last Post: April 21st, 2008, 10:43 PM
  5. Quick javascript question
    By NineOne in forum Webmastering and Programming
    Replies: 2
    Last Post: November 5th, 2002, 12:35 AM

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Recommended Sites: ResellerRatings Store Reviews