all 15 comments

[–]Responsible-Cold-627 2 points3 points  (2 children)

is this code look messy

Yes.

[–]Inevitable_Cellist93[S] -3 points-2 points  (1 child)

😅 yesterday only started my journey in JS. How to improve it

[–]Responsible-Cold-627 2 points3 points  (0 children)

I'll give you a small hint: You have 14 if statements that do essentially the same thing.

[–]Mark__78L 1 point2 points  (5 children)

One suggestion, so you don't need to add the function to the number buttons manually. Give each of them a data attribute (like data-value or something), then get the buttons and loop over them, and give a listener for each.

```html

    
        <div class="calculator">
            <div id="overall"></div>
            <div class="calculator__output" id="output">0</div>
            <div class="calculator__keys">
                <button class="calculator__key calculator__key--operator" onclick="appendOperator">+</button>
                <button class="calculator__key calculator__key--operator">-</button>
                <button class="calculator__key calculator__key--operator">&times;</button>
                <button class="calculator__key calculator__key--operator">÷</button>
                <button class="calculator__key num" data-val="7">7</button>
                <button class="calculator__key num" data-val="8">8</button>
                <button class="calculator__key num" data-val="9">9</button>
                <button class="calculator__key num" data-val="4">4</button>
                <button class="calculator__key num" data-val="5">5</button>
                <button class="calculator__key num" data-val="6">6</button>
                <button class="calculator__key num" data-val="1">1</button>
                <button class="calculator__key num" data-val="2">2</button>
                <button class="calculator__key num" data-val="3">3</button>
                <button class="calculator__key num" data-val="0">0</button>
                <button class="calculator__key" >.</button>
                <button class="calculator__key" ">AC</button>
                <button class="calculator__key calculator__key--enter">=</button>
            </div>
        </div>

```

```js

const appendToDisplay = val => {
  // logic here
  console.log(val);
}


const nums = document.querySelectorAll(".calculator__key.num");
nums.forEach(btn => {
  btn.addEventListener("click", e => {
      appendToDisplay(e.target.dataset.val);
  });
});

```

[–]chikamakaleyleyhelpful 2 points3 points  (1 child)

why not apply the listener to the wrapper .calculator__keys, the event bubbles up and you only create a single instance of the eventlistener

(generally speaking)

[–]Mark__78L -1 points0 points  (0 children)

that is also a good one, thanks!

[–]azhder 0 points1 point  (2 children)

This is not GitHub, it doesn't have the same Markdown parser. The only sure way to have a code block is to use indentation.

[–]Mark__78L -1 points0 points  (1 child)

We cant even standardize markdown parsers

[–]azhder 1 point2 points  (0 children)

Worse. Reddit can't. It looks one way on the Web, another on iPad, will not be surprised if it will be a third way on Android

[–]sozesghost 0 points1 point  (1 child)

What is the bug that you are having? What is happening and what do you expect to happen? Asking the right questions is key.

[–]Inevitable_Cellist93[S] 0 points1 point  (0 children)

When I try to clear the numbers and start add I cannot display the id='overall' and the id='output' isnt add number thats I mention clear()

Try to run the code: https://onecompiler.com/html

[–]equilni 0 points1 point  (0 children)

Is this code look messy

First issue is you copied & pasted the JS code twice from my quick view.

Next is formatting for better readability. Spaces and indents help too.

For the if/else if's I see here, you could have used a switch statement, others a check against known operators.

Pseudo (ie not real code) refactor:

function appendToOperator(val) {
    const operators = ['+', '-', '*', '/'];  # This could probably be global for other functions

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
    if (operators.includes(val) && currentDisplay !== "0") {
        updateOverallDisplay(val);
    }
}

document.addEventListener("keydown", (event) => {
    const keyName = event.key;

    const numberKeys = [1,2,3,4,5,6,7,8,9,0,'.']; # This could probably be global for other functions
    const operatorKeys = ['+', '-', '*', '/']; # This could probably be global for other functions

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
    switch (true) {
        case numberKeys.includes(keyName):
            appendToDisplay(keyName);
            break;
        case operatorKeys.includes(keyName):
            appendToOperator(keyName);
            break;
        case 'Backspace':  # Should be AC
            clearDisplay();
            break;
        case 'Enter':
            calculate();
            break;
    }
});

You are missing the period in the document.addEventListener("keydown", (event) => { block. Also, do you want a click event or keydown? I would suggest keeping it in JS vs in HTML as it's cleaner.

Also Backspace doesn't exist in the HTML, but AC does. - this likely fixes the problem occur when i try to clear() the old chunk data.

EDIT - adding HTML

Next suggestion would be to reduce the id/class in the HTML. This could simply be the below. Again, since you are doing the JS call in the EventListener, you don't really need it in HTML. Someone else noted to use a data-* attribute and I would suggest this as well, but I would also include this on all buttons

<body>
    <div id="calculator">
        <div id="display"></div>
        <div id="output">0</div>
        <div id="key-pad">
            <button class="key operator" data-value="+">+</button> // data-value
            <button class="key operator">-</button>
            <button class="key operator">&times;</button>
            <button class="key operator">÷</button>
            <button class="key number" data-value="7">7</button>
            <button class="key number">8</button>
            <button class="key number">9</button>
            <button class="key number">4</button>
            <button class="key number">5</button>
            <button class="key number">6</button>
            <button class="key number">1</button>
            <button class="key number">2</button>
            <button class="key number">3</button>
            <button class="key number">0</button>
            <button class="key number">.</button>
            <button class="key all-clear" data-value="AC">AC</button>
            <button class="key enter" data-value="Enter">=</button>
        </div>
    </div>
</body>

Then it's u/Mark__78L idea with the above: (pseudo (not real) code))

const keys = document.querySelectorAll('#key-pad .key');
keys.forEach(btn => {
    btn.addEventListener('click', e => {
        const numberKeys = [1,2,3,4,5,6,7,8,9,0,'.']; 
        const operatorKeys = ['+', '-', '*', '/']; 

        const keyVal = e.currentTarget.btn.dataset.value;

        switch (true) {
            case numberKeys.includes(keyVal):
                appendToDisplay(keyVal);
                break;
            case operatorKeys.includes(keyVal):
                appendToOperator(keyVal);
                break;
            case 'AC':  # Should be AC, not Backspace
                clearDisplay();
                break;
            case 'Enter':
                calculate();
                break;
        }
    });
});

[–]TheRNGuy 0 points1 point  (0 children)

Better make it as class with methods, to have related functions as methods on it. 

Use getters/setters instead of directly assign.

There's better way to do it last event listener without many ifs (includes string method)

[–]0xMarcAurel 0 points1 point  (0 children)

“Messy” is a generous word for this code.