2
<script type="text/javascript">
    var num1 = 0;
    var num2 = 0;
    var operator = "";
    var result = document.getElementById("result");
    var history = [];

    function numberClick(num) {
        if (result.value == "0") {
            result.value = num;
        } else {
            result.value += num;
        }
    }

    function operatorClick(op) {
        num1 = parseFloat(result.value);
        operator = op;
        result.value = "0";
    }

    function equalClick() {
        num2 = parseFloat(result.value);
        var answer = 0;
        switch (operator) {
            case "+":
                answer = num1 + num2;
                break;
            case "-":
                answer = num1 - num2;
                break;
            case "*":
                answer = num1 * num2;
                break;
            case "/":
                answer = num1 / num2;
                break;
        }
        result.value = answer;
        history.push(num1 + " " + operator + " " + num2 + " = " + answer);
        document.getElementById("history").innerHTML = history.join("<br>");
    }
    function clearClick() {
        num1 = 0;
        num2 = 0;
        operator = "";
        result.value = "0";
        history = [];
        document.getElementById("history").innerHTML = "";
    }
    document.addEventListener("keydown", keyboardInput);
    function keyboardInput(event) {
        event.preventDefault();
        var key = event.key;
        if (key >= '0' && key <= '9') {
            numberClick(parseInt(key));
        } else if (key === '+') {
            operatorClick('+');
        } else if (key === '-') {
            operatorClick('-');
        } else if (key === '*') {
            operatorClick('*');
        } else if (key === '/') {
            operatorClick('/');
        } else if (key === 'Enter' || key === '=') {
            equalClick();
        } else if (key === 'c' || key === 'C') {
            clearClick();
        }
    }

</script>

Here after getting calculation say 1+1= 2 if we press = again, the result is getting incremented by amount of num1 instead of staying same. Have tried using flag still the issue persist. Also have used break and set a default value still not working.

Can someone please help me with this?

Tried 2+1=3=3=3
Got 2+1=3=5=7=9 so on

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Mar 30 '23 at 18:07
  • I second the use of a debugger, but sometimes it's really hard to do with JavaScript, especially in the browser. You might also look at using ES6 (or newer) code - dropping `var` in favor of `const` and `let`, etc. There's a good bet that these global variables (`var` is global, even when it seems like it shouldn't be) are the root cause - try using more functions than statements, passing values rather than setting variables. – kerry Apr 07 '23 at 06:35

1 Answers1

0

you could just set num1 and num2 to zero after you press equal right?

Henry Kam
  • 11
  • 1