SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    calculator design help please

    i've designed a basic calculator and its working but not how i want it to. i've designed it (and dont want to change the design) so when the the user is inputting their calculation it all displays on the screen e.g. 1+3*5, now when someone puts a calculation in like that it does 3*5 then adds the 1 but i want it so it does the 1+3 first then multiplies by 5.

    can anyone help?

    thanks

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    To overide the standard mathematical order of precedence for performing operations (which states that you always multiply before adding) you surround the part to be done first in parentheses like this:

    (1+3)*5
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my equals function is

    function equals() {

    document.calculator.display.value=eval(document.calculator.display.value);
    }

    i'm not storing the users input as variables , all data in the display is calculated there. is there a statement i can use to do calculations from left to right in the display?

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You would have to write all of the mathematical processing yourself if you want to use a non-standard way of performing the calculations. Also that will confuse anyone using your calculator since anyone with mathematics to a third class level or better knows that 1+3*5=16 and not 20. (I think that is what class my son was in when he learned that you multiply before you add).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

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