React + Redux Calculator Bug Help

I have implmented a feature in my calculator where when two operators are pressed in a row the most recent will override. However despite applying to the final result it displays incorrectly on the calculator screen until a following number is pressed.

e…g. 5,+,- will display a + on the screen until the next number is pressed despite the state being changed

Can anyone help me figure out why i’m losing my hair at this point. Thanks in advance :smile:

Hello Nathan,

After reading some about redux-react stuffs, I just realized it may rerender your app if your state comes with shallow object change.

Let say pop and push over an array won’t make a new array object, instead it changes the state of the array.

You code part is about pop the last operator and push new one won’t trigger react-redux render.

I just came up with following, and it looks working, please check:
TIP: think about a way to push new array, instead of changing existing array.

...
if (ops.includes(state.eq[state.eq.length - 1]) && state.currNum === "") {
        stateHold.eq.pop();
        stateHold.eq.push(action.opStr);
        return {
          currNum: "",
          //eq: stateHold.eq
          eq: stateHold.eq.slice(0,stateHold.eq.length)
        };
...

I also found a very bad bug which is about the way it calculates(eval call), if you just ask for 36 - 6 it will result 3! I think this is becasue of bug with your redundZero. You should just remove the zero from floating part.

Following

if (str[index]==='0' || str[index]==='.'){
      index--;
      continue;
    }

Is faulty, please note once you reach the dot (.) (and probably becasue all float parts were zero), you should go one more index-- and break, becasue rest zeros(at left side) are required. (quick math :smile: ), please fix it.

I also two suggestions.
0) please make it possible to work with keyboard, it’s much easier to work with a calc with keyboard, rather mouse click.

  1. Once user enters an operator (+, - , …) have small place to show the previous operators, and values in queue somewhere, this helps so much.

Keep going on great work comrade, happy programming.

1 Like

Hello Nathan,

I have a favor, I know this could be much, but for people having same state issue I would ask you:

Would you may please keep the faulty line of code about the state as comment, and with some small description on your code please?

Thank you so much, really appreciate it.