# Calculator Project Feedback

**URL:** <https://forum.freecodecamp.org/t/calculator-project-feedback/760932>\
**Category:** I Built This\
**Created:** [September 5, 2025, 4:18am UTC](https://forum.freecodecamp.org/t/calculator-project-feedback/760932 "2025-09-05T04:18:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![smiscenich](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/smiscenich/32/392004_2.png) [@smiscenich](https://forum.freecodecamp.org/u/smiscenich)\
**Post date:** [September 5, 2025, 4:18am UTC](https://forum.freecodecamp.org/t/calculator-project-feedback/760932/1 "2025-09-05T04:18:24Z")

</div>

Hey campers,

I just completed my first independent project after finishing the HTML, CSS, and JS sections of the curriculum. It’s a simple calculator app that supports memory, deletion, clear, decimals, and sign changes. How did I do?

I’d love tips on how to refactor `buildExpression()` to be more DRY. It’s super repetitive, what with all the `leftSide` and `rightSide` checks! But, I’m stumped as to a better approach. I’m sure it’s super obvious.

[Here’s a link to my calculator](https://goodsaw.github.io/smiscenich.github.io/Calculator/)

[Here’s the complete source code (HTML, CSS, JS) on gitHub](https://github.com/GoodSaw/smiscenich.github.io/tree/main/Calculator)

```auto
const numberParent = document.querySelector(".numbers");
const numbers = numberParent.children;
const operatorParent = document.querySelector(".operators");
const operators = operatorParent.children;
const display = document.getElementById("display");
const equalsButton = document.querySelector(".equals");
const calculator = document.querySelector(".calculator-container");
const body = document.querySelector("body")

let leftSide = '';
let rightSide = '';
let operation = '';
let isOperated = false;
let solution = '';

function handleFloat(string){
    if (string.includes(".")){
        return string;
    } else {
        return string+=".";
    }
    
}

function changeSign(string){
    if (!string) {
        return '';
    }
    return (parseFloat(string) * -1).toString()
}

function deleteCharacter(string) {
    let arr = string.split("")
    arr.pop()
    return arr.join("");
}

function setMemory(){
    let value = parseFloat(display.innerText)
    if (typeof value === "number" && !isNaN(value)){
        sessionStorage.setItem("memory", value);
        }
}

function getMemory(){
    if (!isNaN(sessionStorage.getItem("memory"))){
        return sessionStorage.getItem("memory");
    } 
}

function handleInput(e) {
    let input = ''
    if (e.type === "keydown") {
        let inputRegex = /=|\d|\*|\+|\-|\/|\./gi
        if (e.key === "Backspace" || e.key === "Enter") {
            input = e.key;
        }
        if (inputRegex.test(e.key)) {
            input = e.key
        }
    } else if (e.type === "click") {
        input = e.target.value;
        
    }
   
    return input; 
}

function handleClear(){  
    leftSide = '';
    rightSide = '';
    solution = '';
    operation = '';
    isOperated = false;
}

function updateDisplay() {
    display.innerText = leftSide+operation+rightSide;
    if (solution || parseInt(solution) === 0) {
        display.innerText = solution;
    }
}

function buildExpression(input) {
    if(display.innerText === "DIVIDE BY ZERO ERROR"){
        display.innerText = "";
    }
    if(/\d/ig.test(input)){
        if (isOperated === false) {
            leftSide += input;
        } else {
            rightSide += input;      
        }
    } else if (/\*|\+|\-(?![a-z])|\//.test(input)) {
       
        if (typeof parseFloat(display.innerText) === "number" & !isOperated) {
            leftSide = display.innerText;
        }
         if (!leftSide || rightSide) {
            return;
        } 
        isOperated = true;
        operation = input;
             
    } else if (input === ".") {
        if (isOperated) {
            rightSide = handleFloat(rightSide)
        } else {
            leftSide = handleFloat(leftSide)
        }
        
    } else if (input === "sign") {
        if (isOperated) {
            rightSide = changeSign(rightSide)
        } else if (leftSide === ""){
            leftSide = changeSign(parseFloat(display.innerText))
        } else {
            leftSide = changeSign(leftSide)
        }
    } else if (input === "Backspace") {
         if (isOperated) {
            if (!rightSide){
                operation = '';
                isOperated = false;
            }
            rightSide = deleteCharacter(rightSide);
        } else {
            leftSide = deleteCharacter(leftSide)
        }
    } else if (input === "clear") {
        handleClear();
        display.innerText = '';
        localStorage.clear;
    } else if (input === "memory-recall"){
        if (isOperated) {
            rightSide = getMemory() ? getMemory() : rightSide;
        } else {
            leftSide = getMemory() ? getMemory() : leftSide;
        }
    }
    updateDisplay()
}

function evaluateExpression(){
    if (leftSide && !rightSide) {
        return leftSide;
    } else {
        let parsedLeft = parseFloat(leftSide)
        let parsedRight = parseFloat(rightSide);
        let result;
        switch (operation) {
            case '+':
                result = parsedLeft + parsedRight;
                break;
            case '-':
                result = parsedLeft - parsedRight;
                break;
            case '*':
                result = parsedLeft * parsedRight;
                break;
            case '/':
                if (parsedRight === 0) {
                    result = "DIVIDE BY ZERO ERROR"
                    break;
                }
                result = parsedLeft / parsedRight;
                break;
        }
        isSolved = true;
        solution = result;
        updateDisplay();
    };
    handleClear();
}

calculator.addEventListener("click", (e)=> {
    if (handleInput(e)==="memory-set"){
        setMemory()}
    buildExpression(handleInput(e))
    if (handleInput(e)==="=") {
        evaluateExpression()
    }
})
body.addEventListener("keydown", (e)=> {
    buildExpression(handleInput(e))
    if (handleInput(e)==="="||handleInput(e)==="Enter") {
        evaluateExpression()
    }
})

```

---

<div class="post-metadata">

**Author:** ![sanity](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sanity/32/378897_2.png) [@sanity](https://forum.freecodecamp.org/u/sanity)\
**Post date:** [September 5, 2025, 5:56pm UTC](https://forum.freecodecamp.org/t/calculator-project-feedback/760932/2 "2025-09-05T17:56:54Z")

</div>

Having working code is a great starting point for improvements.

You are right `buildExpression` is looking a bit complicated, without a clear way to make it simpler. Perhaps instead of multiple times checking `isOperated`, checking it earlier would decide whether to handle _left_ or _right_ side.

Remember also to try to split changes into smaller steps. This helps with keeping things working during the modifications.

---

<div class="post-metadata">

**Author:** ![pkdvalis](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pkdvalis/32/350571_2.png) [@pkdvalis](https://forum.freecodecamp.org/u/pkdvalis)\
**Post date:** [September 5, 2025, 8:50pm UTC](https://forum.freecodecamp.org/t/calculator-project-feedback/760932/3 "2025-09-05T20:50:01Z")

</div>

I like the cactus theme 🌵

I was pleasantly surprised to find the numpad working for input. However when I pressed “enter” it clears when I was expecting to see the result. I have to press the “=” key on the keyboard which is a bit awkward when doing calculations using the numpad.

Oops, enter appears to work now.

Or is it? If I press 55+5 with the mouse and hit enter the screen says 5.

If I keep hitting enter it keeps putting 5s into the screen.

I would expect the `del` key to clear the screen. Backspace works well.

If I click AC then on numpad “55+5 enter” it clears the screen.

So there’s a bit of UX hiccups here to look at.

If I refresh the page then on numpad “55+5 enter” it shows 60 ✅ But if I use the mouse then go back to keypad it no longer works correctly.

---

<div class="post-metadata">

**Author:** ![archana115](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@archana115](https://forum.freecodecamp.org/u/archana115)\
**Post date:** [September 20, 2025, 1:28am UTC](https://forum.freecodecamp.org/t/calculator-project-feedback/760932/4 "2025-09-20T01:28:41Z")

</div>

> [@smiscenich](#):
>
> I just completed my first independent project after finishing the HTML, CSS, and JS sections of the curriculum. It’s a simple calculator app that supports memory, deletion, clear, decimals, and sign changes. How did I do?
> 
> I’d love tips on how to refactor `buildExpression()` to be more DRY. It’s super repetitive, what with all the `leftSide` and `rightSide` checks! But, I’m stumped as to a better approach. I’m sure it’s super obvious.

Some feedback:

1. Instead of manually checking `*|+|-|/` with regex, keep a `Set`.  
const validOps = new Set([‘+’, ‘-’, ‘\*’, ‘/’]);

2. `updateDisplay()` currently mixes raw expression and solution logic. A cleaner split is:.  
function expressionString() {  
return sides.left + operation + sides.right;  
}

3. `localStorage.clear;` → doesn’t actually clear memory. You probably meant `localStorage.clear()`.?

---

<div class="post-metadata">

**Author:** ![Naomi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naomi/32/422181_2.png) [@Naomi](https://forum.freecodecamp.org/u/Naomi)\
**Post date:** [January 13, 2026, 7:01pm UTC](https://forum.freecodecamp.org/t/calculator-project-feedback/760932/6 "2026-01-13T19:01:13Z")

</div>

Closed on January 13, 2026, 7:01pm UTC


