I noticed a bug with your = when you press it repeatedly.
Example: 6x3 = 18 then press = a few more times and it all goes squiffy when you then enter another operator.
Essentially you rerun your functions every time the = is clicked. You may need to add a logical safeguard to this.
FWIW, this was a tricky project for me to complete. I took an entirely different approach, using React and states to manage building of maths expressions and to safeguard against erroneous button presses, but my code was verbose and probably not especially elegant!
and passed all tests even before fixing bug that you said. It literally took me three days to figure that out.
BTW, thanks both of you for pointing out my mistakes and helping me.
There are a lot of libraries that you can use (Xstate, redux, etc).
The basic idea is that the app has a state, and can only transition to the next state if the “correct button is pressed” (event). So, we have a current state, an event and the next state (plus a function to move from one state to another):
const states = {
// RED is a state
'RED': {
// tick is an event (button press)
'tick': 'GREEN' // GREEN is the next state
},
'YELLOW': {
'tick': 'RED'
},
'GREEN': {
'tick': 'YELLLOW'
},
}
// We pass the current state and an event: we get the next state
const transition = (state, event) => states[state][event];
console.log(transition('RED','tick')); // GREEN
console.log(transition('YELLOW','tick')); // RED
console.log(transition('GREEN','tick')); // YELLOW
// If the event (button press) is incorrect we get undefinded
console.log(transition('RED','asdf')); // undefined
The above code is only an example, there are better ways to implement this idea. I had more examples in my Portfolio