How to implement memory functions (MR, MC, M+, M-) in a JavaScript calculator

I’m currently developing a calculator and want to incorporate memory features. Specifically, I need to implement four memory functions: memory recall (MR), memory clear (MC), memory add (M+), and memory subtract (M-).

My plan is to create separate functions for each memory operation and to use distinct variables to maintain the memory values. Is this a conventional method for handling memory in calculators, or is there a more optimized way?

Additionally, I would appreciate it if someone could check my code and point out any potential errors. The display for the calculator is managed by an element with the ID ‘number-display’.

$(document).ready(function(){
    var screenValue = "";
    var mathOperators = ["/", "*", "-", "+"];
    var hasDecimal = false;

    $("button").click(function() {
        var buttonText = $(this).text();

        function refreshScreen() {
            if (screenValue.length > 19) {
              $("#number-display").html(screenValue.substr(screenValue.length - 19, screenValue.length));
            } else {
              $("#number-display").html(screenValue.substr(0, 19));
            }
        }

        if (buttonText === "AC" || buttonText === "ON" || buttonText === "MC") {
            hasDecimal = false;
            screenValue = "";
            $("#number-display").html("0");
        }
        else if (buttonText === "OFF") {
            hasDecimal = false;
            screenValue = "";
            $("#number-display").html("");
        }
        else if (buttonText === "CE") {
            if (screenValue.substr(screenValue.length - 1, screenValue.length) === ".") {
                hasDecimal = false;
            }
            screenValue = screenValue.substr(0, screenValue.length - 1);
            refreshScreen();
        }
        else if (!isNaN(buttonText)) {
            screenValue += buttonText;
            refreshScreen();
        }
        else if (buttonText === ".") {
            if (!hasDecimal) {
                if(screenValue > 0){
                    screenValue += buttonText;
                }
                else {
                    screenValue += "0" + buttonText;
                }
                hasDecimal = true;
                refreshScreen();
            }
        }
        else if (mathOperators.indexOf(buttonText) > -1) {
            hasDecimal = false;
            if (screenValue.length > 0 && !isNaN(screenValue.substr(screenValue.length - 1, screenValue.length))) {
                screenValue += buttonText;
                refreshScreen();
            }
            else if (screenValue.length === 0 && buttonText === "-") {
                screenValue += buttonText;
                refreshScreen();
            }
        }
        else if ($(this).id === "sqrt") {
            var tempValue = screenValue.html();
            $("#number-display").html(eval(Math.sqrt(tempValue)));
            hasDecimal = false;
        }
        else if ($(this).id === "plusmn") {
            var negativeNum = screenValue * -1;
            $("#number-display").html(negativeNum);
        }
        else if (buttonText === "M-") {
            // Memory subtract implementation needed
        }
        else if (buttonText === "M+") {
            // Memory add implementation needed
        }
        else if (buttonText === "%"){
            // Percentage function needed
        }
        else if (buttonText == "=") {
            if (isNaN(screenValue.substr(screenValue.length - 1, screenValue.length))) {
                screenValue = screenValue.substr(0, screenValue.length - 1);
            }
            var result = screenValue;
            result = eval(result);
            screenValue = String(result);
            if (screenValue.indexOf('.')) {
                hasDecimal = true;
            } else {
                hasDecimal = false;
            }
            $("#number-display").html(screenValue);
        }
    });
});

just create a global variable like var memoryValue = 0; then for MR set screenValue to memoryValue, MC sets memoryValue to 0, M+ does memoryValue += parseFloat(screenValue) and M- does memoryValue -= parseFloat(screenValue). dont forget to refresh screen after each operation. btw your sqrt function has a bug - should use screenValue not tempValue.html()

There’s actually an issue with how you’re handling the memory clear function. Currently your MC button is clearing the screen display but not actually clearing the memory storage itself. You need to declare a memory variable at the global scope and then properly manage it across all memory operations. For the implementation, I’d suggest adding var calculatorMemory = 0; near your other variable declarations. Then for MR you’ll want to set screenValue to calculatorMemory and refresh display. M+ should add the current display value to calculatorMemory using calculatorMemory += parseFloat(screenValue || 0). M- works similarly but subtracts. Don’t forget to update your MC condition to include calculatorMemory = 0; so it actually clears memory. Also spotted another bug in your equals function - screenValue.indexOf('.') will return 0 for numbers starting with decimal, not -1 for no decimal. Should be screenValue.indexOf('.') !== -1 to properly set hasDecimal flag.

Your approach is correct but I’d recommend creating the memory variable outside the click handler to maintain state properly. Add var memoryValue = 0; at the top level with your other variables. For the memory functions implementation, you’ll need to handle the current display value conversion carefully. When implementing MR, check if memoryValue exists before displaying it. For M+ and M-, make sure to parse the current screen value or use eval() result if there’s a pending calculation. Also noticed your MC button already clears the screen but doesn’t reset memory - you should add memoryValue = 0; to that condition. The plusmn function also needs to update screenValue variable, not just the display.