Help with React JS Calculator

Hi everyone! I need help with the last requirement for my React JS Calculator I have struggled to make this for a while and moved on to other projects, but now I think it’s time to finish this one.

It passes all the requirements except for:

13. If 2 or more operators are entered consecutively, the operation performed should be the last operator entered

If someone can help me find a solution with the way I’ve already created it I will really appreciate it.
I know there are probably better ways to code a calculator but this has already taken a lot of time and I’m almost done.

Here it is:

When Handling Equal:
Take your input data, check if theres 2 or more operators consecutively (u can use regex); if so, replace it (but not the last one) with an empty string “”. Then proceed to make the eval() with the new string

That is brilliant! So I’ve tried to implement it into my code and so far I have this:

handleEqual() {
    let twoOper = /([+\-*/]{2}\1)/g;
    if (twoOper.test(this.state.input) == true) {
      let one = this.state.input.match(twoOper)
      let hold = this.state.input.replace(one[0][0], '')
      this.setState({ input: eval(hold)})
    } else {  
      this.setState({ input: eval(this.state.input)});

I’m still having issues finding a working solution,

  • one being this regex works if I know the number of consecutive operators beforehand. But if it goes over or under the number it stops working.

  • Also, the way I’m replacing it is not ideal since I don’t know how to find out the operators beforehand I’m using the match() function and targeting the first element of the array it creates and replacing it with blank space, and because I have \1 at the end of the regex it repeats as necessary but I don’t know how to target it since it could be one array, two, or even more.

So if you have any idea on how I can fix this that would be great, but I’m gonna keep trying until then.
Thank you @Kurtony for the idea!

First, I would write clear Pseudo Code.
Then I would read about capture groups and how to use $n.

1 Like

I agree with Miku, reading about regexp is not a bad idea; even I have to read again just to remember well. To be curious and honest, Idk if theres a way to match all but the next one using regex. Nevertheless, regex is still useful in this case.

With that said check this out, I wrote it in your code and worked.

  • Take input
  • spread
  • check every character of input AND the next one of the one the you are checking
  • if needed, replace the one that you must replace
  • join
  • eval()

If that solution is not enough for you, I have no problem sending you the code so you can fully understand what I mean.

1 Like

Thank you @miku86 and @Kurtony! I really appreciate it I ended up finding a solution it’s quite a tricky task but this is what I did:

 handleEqual() {
    let twoOper = /[+\-*/]/;
    let can = [];
    let box = [...this.state.input]
    for(let i = 0; i < box.length; i++) {
      if (twoOper.test(box[i]) == true && twoOper.test(box[i + 1]) == true) {
      } else {
    this.setState({ input: eval(can.join(''))}) 

So I followed the psuedo code you said I think it varies in the fact that instead of replacing the previous element I just made a new array and controlled the input that was sent to that one with an if statement.
I was hesitant to ask you for the code since I wanted to try to figure it out. But now I wouldn’t mind using it for comparison and see where I can improve. But Thanks again I really appreciate it! :+1:t4:

1 Like

I think you can remove == true in both places

Your right I forget it’s not necessary if it should be true but better safe than sorry. I did change it though so thanks!