Calculator project-problem with "-" symbol

I have finished the Calculator Project using React only to find out at the end that it passes 15 of the 16 tests. It does not pass the 13th test regarding the calculation of 5*-+5 = 10 !!

What I am trying to do in order to fix the problem is use RegExp so that it neglect specific symbols but I have not succeeded.

Any suggestions on a RegExp that could work? Or maybe an alternative that I havent thought about.

Thx in advance guys

Here you can find my code in codepen:

1 Like

Without looking at the code, it seems like you have to keep track if the user is typing in an operand. If he is, then you only accept the most recent operant typed in that string.

1 Like

Something similar to the following may or may not help:

// if there are more operators than numbers
    // remove the first operators
    if (operators.length > numbers.length) {
      operators.splice(0, numbers.length);

Well…I did that in the first place but the test does not accept that. It requests the result of 5*-5 to be -25. At the same time it requests the result of 5*-+5 to be equal to 10.

So what’s your best idea for an algorithm to solve this?

1 Like

I was thinking of a RegExp that can ‘read’ the final input (e.g. 5*-+5) and return the string excluding all symbols except the last one (e.g. 5+5) except the case of 5*-5 where i want the two symbols to remain.

I havent been able to find such a Regular Expression though.