Ready to bang my head against the wall! JS Calculator problems

At this point, I am frustrated and ready to bang my head against the wall. But I don’t want to give up and abandon my JS Calculator. I’ve been working on this problem the past few days and have gotten few helps and tips from FreeCodeCamp chat and Stackoverflow to bring me this far but not enough to solve this problem.

The problem I’ve been working on is repeating operators. For example, if I press “5” and then “+”, but changed my mind and pressed “-”, the history area should display “5-” instead of “5+”.

In my original code at this Original JS Calculator Codepen, everything else works fine. However, in this codepen, If I press “5” and “+”, but then press “-”, the history display “5+0-”.

I was able to fix this problem in this Forked JS Calculator 3 Codepen. However, it created even more problems. For example, if I use multiply mathematical operations like “1+2+3”, the history display doesn’t work properly.

At this point, I just want someone to write the solution so that I can see what I overlooked or didn’t think about.

Also I have added console.log to most statements in my code. It’s to help me figure out what’s wrong with my code. I will clean it up once I fix all the problems found…

Btw, all the action is happening in function addOperator(keyitem)

Looking forward to solutions so that I don’t need to bang my head against the wall… T_T

If you just want to see solutions which work, then you can look at the demo’s code or visit the Let’s discuss your “JavaScript Calculator” thread on this forum. You will see multiple successful approaches to this project. You can compare your solution to the others and hopefully see where you could have approached the project differently.

As I tried to explain in my various replies to your other posts, your logic was difficult to follow and your functions were trying to do too many things with various global variables, which ultimately caused various “side-effects”.

I would recommend starting over after writing out the various algorithms for this challenge in plain language before attempting to code it again. Maybe with a fresh start you will come up with a simple solution which was impossible to do when you were trying to bandage an existing complex solution.