React Calculator unable to pass multiple operator test

Hi, I’ve built a react calculator for the font end library certificate but I can’t finish it.

The only way I have been able to get it to work this far is to chain if statements, it’s a mess. I’ve tried everything I can think of to make it more elegant but no mater how much I try to use functional programming ideals it becomes more convoluted.

I have made some branches and tried to redesign it from the ground up but I keep coming back to the same issue. It ends up with ifs within ifs. At this point I just want to get it to pass the tests. Can anyone suggest how I could get it to pass in it’s current state without needing to start from scratch.


It looks like the only test you are not yet passing is User Story 13. If I break it down for you, maybe that will help you determine how to code it?

  1. If the screen reads 5* and I press the - key, the screen should read 5*-.
  2. If the screen reads 5*- and I press the - key, the screen should read 5-.

Hope this helps!

Hi Nh, thanks for looking at it for me. I understand that but I don’t know how to do that without ripping it all down. The minus is the part that’s causing the issue. I need a way to replace three in a row with the last but I can’t capture that possibility because the code as it is doesn’t let that happen…

if (symbolsRegex.test(display.slice(-2, -1)) && val !== "-")

You have an if loop here that checks if there’s already an operator on the display, but doesn’t run if I press the - button.

Before that if loop, though, you need another if loop that handles something like 6+- on the screen. What should that loop do when I press an operator? Remember that you can use string.charAt() to test specific positions in your display.

I didn’t even think of the charAt method, yeah that would make more sense than using slice. The main issue is that if I console.log the display while these check as happening it seems to be a step behind. If you do a console.log of the last char at the place you suggested it is showing what it was before the button was pressed. Also has the issue that you can’t just do length -1 because some time there is a space sometimes not. (minor issue I know)