Make a JavaScript Calculator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • StoneCold
    New Member
    • Jan 2013
    • 2

    Make a JavaScript Calculator

    Hey guys, I need to make a calculator
    I have buttons for 0-9, digits = button, . button and have +, -, *, /, modules and square root operators and CE, M+, M- and backspace.
    I have made the interface. Now all I want is make the javascript code.
    It will be simple, I need a function for each and every operator. and also a switch case. can you guys plz help me??? thank you all..........
    1 Minute Ago #1
  • Jwalker
    New Member
    • Jan 2013
    • 3

    #2
    Use "eval()" function.
    Each button writes his symbol in a textbox. Then evaluate the text entered using the eval function.
    Try to start it

    Comment

    • sainathsagar
      New Member
      • Jan 2013
      • 7

      #3
      eval() function is quite easy to use !!

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        eval() function is quite easy to use !!
        eval() function is quite easy to abuse !!

        if you’re not extremely careful, any attacker has your site in his hands.


        PS. be aware of floating point arithmetics’ traps
        Code:
        // e.g.
        var sum = 0.1 + 0.2;
        alert(sum == 0.3);

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          I need a function for each and every operator. and also a switch case.
          What have you managed have so far? Give your attempt for, e.g., the + and = buttons. This will make it easier for someone to help you.

          Comment

          • Anas Mosaad
            New Member
            • Jan 2013
            • 185

            #6
            eval() function is quite easy to abuse !!

            if you’re not extremely careful, any attacker has your site in his hands.

            Dormilich, just to remind you. JavaScript is client side. I don't believe that an attacker can do something harmful to his site with eval(). However, it's a good programming practice to handle all such issues.

            Comment

            • StoneCold
              New Member
              • Jan 2013
              • 2

              #7
              @all Guys I don't need to use eval function. Teacher said don't use it.
              here is my = function
              Code:
              function equals() {
              CurentDisplayValue = eval(document.frmOne.txtDisplay.value)
              PreviousDisplayValue = eval(total)
              
              if(TheOperator =="+") {
              	answer = PreviousDisplayValue + CurentDisplayValue
              }
              else if(TheOperator == "*") {
              	answer = PreviousDisplayValue * CurentDisplayValue
              }
              else if(TheOperator == "-") {
              	tanswer = PreviousDisplayValue - CurentDisplayValue
              }
              else if(TheOperator == "/") {
              	answer = PreviousDisplayValue / CurentDisplayValue
              }
              document.frmOne.txtDisplay.value = answer
              }
              how to create the equall buttun without eval?
              Last edited by Dormilich; Jan 8 '13, 06:38 AM. Reason: Please use [CODE] [/CODE] tags when posting code.

              Comment

              Working...