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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript Calculator: Parentheses

    I need help putting parentheses in my calculator. Could anyone provide me some source code for parentheses in a calculator?

    If not source code, could you offer me recommendations as to how to solve my problem? Thanks!

  • #2
    Regular Coder
    Join Date
    Mar 2011
    Posts
    147
    Thanks
    35
    Thanked 0 Times in 0 Posts
    Could you explain your problem more please?

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,142
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    If you ask JavaScript (or any programming language) to perform a calculation using multiple operators, those operators will be evaluated in a specific order. For example 3 + 6 * 7 is calculated as ( 6 * 7 ) + 3 because the * is calculated before the +. The order in which these are evaluated is: * / % + - + (where the second + is appending strings). To change the order in which they are calculated, use parenthesis ( ) as the contents of parenthesis are calculated before the contents outside the parenthesis. For example, 3 + 6 * 7 = 45 but ( 3 + 6 ) * 7 = 63.

    If this explanation is insufficient, try Google.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Regular Coder
    Join Date
    Mar 2011
    Posts
    147
    Thanks
    35
    Thanked 0 Times in 0 Posts
    Do you want to make it so that you can change the order of operations with parentheses?

    For example, if you were using 4 + 2 * 5 and wanted to add 4 to 2 and then multiply that (6) by 5, then it would look like this: (4 + 2) * 5

    If so, you can make a string "(4 + 2) * 5"

    And then do

    Code:
    var equation = "(4 + 2) * 5";
    var answer = parseInt("(4 + 2) * 5");
    alert(answer);

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,097
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb Minor modification ...

    Works better without the '"' around the string.
    Code:
    <script type="text/javascript">
    var equation = "(4 + 2) * 5";
    var ianswer = parseInt((4 + 2) * 5);
    var fanswer = parseFloat((4 + 2) * 5);
    var irat = parseFloat(355/113);
    alert(equation+'\n'+ianswer+'\n'+fanswer+'\n'+irat);
    </script>


  •  

    Posting Permissions

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