JavaScript Calculator - feedback wanted!

JavaScript Calculator - feedback wanted!
0.0 0


I finally finished this project after so much sweat, blood and tears…

Please provide some feedback and pointers on how to make the codes less convoluted. I really feel that the structure is shaky, even though the calculator works.

Thanks a lot fellow campers!


If I type 2*8= and then hit the +/- button and then type +10= I expect to see -6, but instead I get 26. Your +/- button is not working as it should.


Thanks for testing it out!
OK, so I’ve defined the function pressNegate() as this:

function pressNegate(){
if(result !== 0){
        tempArr = [result - (result*2)];
        document.querySelector(display).textContent = result;
    } else {
        //toggle the previously pressed number(s) between + and -

Is there a way to toggle without using jquery though? or do I simply have to test using if/else statement?


I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

No jQuery needed. What about multiplying the current result by -1? If result is positive, it will become negative. If result is negative, then it will become positive.


Sorry, should have remembered that!

So I think I fixed it (and I’ve modified the code so that each time the equals operator is pressed, the calculator only displays the result, not the equation), but there is one small problem remaining.

Say, I do this: 5 + 3 =

The calculator will display 8

If I then press another operand, say a minus, the sub display will show this: 8, -

I don’t know where that comma after the number comes from. Even weirder, when I console logged it, there`s no comma to be seen.

The codepen url again:


I would have to study your current code more, but my guess is you have a += between an array and a string and the result is a string. The comma is the separator of the array. When an array gets coerce into a string, it keeps the comma. For example:

var arr = [1, 2, 3];
var str = 'fun';
arr += str; // arr becomes '1,2,3fun'