Javascript计算器算术bug和小数点

时间:2016-01-11 15:43:26

标签: javascript html function calculator

问题#1 - 算术错误 我建立了一个非常实用的计算器,除了在大约三或四次减法运行后它开始返回错误答案的事实。我假设存储的值存在问题,但我无法弄明白。

问题#2 - 小数点不起作用 我只是无法弄清楚如何在混音中得到我的小数点按钮。我是一个相当绿色的程序员,但我做了我的研究。

HTML

    <div id="container">
        <div id="calculator">
            <div id="top-row">
                <button id="clear" onclick="clearMemory()" value="">
                    C
                </button>
                <input id="display" type="number" maxlength="6">
                </input>
            </div>
            <div id="second-row">
                <button id="seven" onclick="handleNumberClick(7)" value="7" class="number">
                    7
                </button>
                <button id="eight" onclick="handleNumberClick(8)" value="8" class="number">
                    8
                </button>
                <button id="nine" onclick="handleNumberClick(9)" value="9" class="number">
                    9
                </button>
                <button id="plus" onclick="handleOperationClick('add')" value="+" class="op">
                    +
                </button>
            </div>
            <div id="third-row">
                <button id="four" onclick="handleNumberClick(4)" value="4" class="number">
                    4
                </button>
                <button id="five" onclick="handleNumberClick(5)" value="5" class="number">
                    5
                </button>
                <button id="six" onclick="handleNumberClick(6)" value="6" class="number">
                    6
                </button>
                <button id="minus" onclick="handleOperationClick('subtract')" value="-" class="op">
                    -
                </button>
            </div>
            <div id="fourth-row">
                <button id="one" onclick="handleNumberClick(1)" value="1" class="number">
                    1
                </button>
                <button id="two" onclick="handleNumberClick(2)" value="2" class="number">
                    2
                </button>
                <button id="three" onclick="handleNumberClick(3)" value="3" class="number">
                    3
                </button>
                <button id="divide" onclick="handleOperationClick('division')" value="/" class="op">
                    /
                </button>
            </div>
            <div id="fifth-row">
                <button id="zero" onclick="handleNumberClick(0)" value="0" class="number">
                    0
                </button>
                <button id="decimal" onclick="handleNumberClick('.')" value="." class="number">
                    .
                </button>
                <button id="equals" onclick="handleOperationClick()" value="=" class="number">
                    =
                </button>
                <button id="multiply" onclick="handleOperationClick('multiplication')" value="x" class="op">
                    x
                </button>
            </div>
        </div>
    </div>

的JavaScript
           

    var operatorPressed = false;
    var prevOperand = 0;
    var currentOperand = 0;
    var operationRequested = '';
    var decimalAdded = false;

// Creates calculator display input
    var displayNumbers = document.getElementById("display");  

// Clears calculator display and var a values
    function clearMemory()  {
      displayNumbers.value = '';
      prevOperand = 0;
      //var a = 0;
      //console.log(a);
    };

    function clearDisplay() {
      displayNumbers.value = "";
    };

// Displays values on calculator screen
    var displayValue = function(num)    {
        if (displayNumbers.value.length > 9)  {
            displayNumbers.value = "ERROR";
        } else    { 
            displayNumbers.value = num;
        };
    };

    function handleNumberClick(num) {
        currentOperand = operatorPressed ? num : displayNumbers.value += num;
        operatorPressed = false;
        displayValue(currentOperand);
        console.log(num);
    };

    function clearNumberEntered(){
      numberEntered='';
      clearDisplay();
    }

// Operators function
    function handleOperationClick(operator){
        var result;
        operatorPressed = true;
        switch(operationRequested){
            case 'add':
                result = add(prevOperand, currentOperand);
                break;
            case 'subtract':
                result = subtract(prevOperand, currentOperand);
                break;
            case 'multiplication':
                result = multiply(prevOperand, currentOperand);
                break;
            case 'division':
                result = divide(prevOperand, currentOperand);
                break;
            default:
            result= '';
        }
        if(result){  //if an acutal computation occurs, we'll store overwrite the result to the prevOperand
            displayValue(result);
            prevOperand = result;
            operationRequested = '';
        } else {  //if no computation occurs we'll just set the input val as the prevOperand
            prevOperand = currentOperand;
            operationRequested = '';
        }
        console.log("operation:%s %d to %d", operationRequested, currentOperand, prevOperand );
        operationRequested = operator || operationRequested;
    }

    function add(num, adder){
        var sum = parseInt(num) + parseInt(adder);
        return sum;
    }

    function subtract(num, subtracter)  {
        var difference = parseInt(num) - parseInt(subtracter);
        return difference;
    }

    function multiply(num, multiplier)  {
        var product = parseInt(num) * parseInt(multiplier);
        return product;
    }

    function divide(num, divisor)   {
        var quotient = parseInt(num) / parseInt(divisor);
        return quotient;
    }


    </script>

0 个答案:

没有答案