0

ISSUE #1--Arithmetic bug I built a pretty functional calculator, except for the fact that after about three or four runs of subtraction it starts returning the wrong answer. There is something wrong with the values I am storing I assume, but I just cant figure it out.

ISSUE #2--Decimal point not working I simply just can't figure out how to get my decimal point button in the mix. I am a fairly green programmer, but I have done my research.

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 Answers0