JS - React Calculator - Equal Sign Logic

Hi guys i need some help with the react calculator, here’s my code so far: https://codepen.io/mav1283/pen/VGLxJy?editors=0010 i figured out how to calculate the input equation every time i hit the equals sign but i’m having a hard time trying to solve or set the logic behind so that every time i enter a number it will replace the initial value but if i enter an operator it will re-use the old calculated value… I haven’t added the % and the positive/negative functionalities yet, i’ll add it as soon as i get the logic behind the equals sign. Also the state display above is temporary so i can see the changes of each actions. Need help thanks!

Create another variable “x” who gets value only when expression is calculated i.e. 2+2 -> var exp=true; if pressed clear exp=false. If its true and you enter number, erases numerical value from given field and replaces it with your number, if its false, you’ll just add pressed operator on string in your input field on rest of mathematical expression.

1 Like

@codename11 the variable x is another state right? and the equalsFlag is set to true after i hit equals, so i’ll store another copy of the calculated value to x?

No, x just remembers if it have previously calculated value, not what is value. For that, use another one.

1 Like