Build a JavaScript Calculator user story 13

Tell us what’s happening:

Hit the wall here … please help me if you can, don’t mind my ugly code, for now i’m just trying to pass all the tests, 15 is my max :confused:
can’t wrap my head around user story 13.

Tried to keep track of operators’ number in state, and catch it with regex and slicing input, than with conditions starting with input empty|not empty , with operators '-' |!'-'

Your code so far
codesandbox

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.

Challenge: Build a JavaScript Calculator

Link to the challenge:

Hi @Annestezia,

great work so far!


User Story 13 says:

User Story #13: If 2 or more operators are entered consecutively, the operation performed should be the last operator entered (excluding the negative (-) sign). For example, if 5 + * 7 = is entered, the result should be 35 (i.e. 5 * 7); if 5 * - 5 = is entered, the result should be -25 (i.e. 5 x (-5)).

Do you understand the task properly?
Can you describe it in your own words?


Debugging:
When I input 2-*3,
I get the error: SyntaxError: expected expression, got '*'.

Do you know what this error means?
Do you know why it comes up?

Hi, Michael,
thank you for your time!

I only left basic part uncommented (add operator characters to input) .
Honestly this user story is not completely clear to me, i see what behavior is expected in the tests, but e.g. should 5--3 be 5-3 or 5+3 (mathematically correct version)?
there are 3 options for newly added characters: add, replace,ignore, 2 groups of operators '-' and !'-'
when i enter operators except for '-' consecutively the last entered operator is used.

'-'
replaces preceding +, not sure abt -
added to input after * or /

if the 3rd operator is entered after *- or /-, use the last entered operator.

I can’t generalize these conditions :confused: i’m pretty sure there’s something else…
Also when i tried to keep track of number of consequtive operators in state, it’s not quite what I expected, there’s clearly an operator in the input, but in state it’s 0. Is this the time when i need functional setState?

2-*3 hmm it makes no sense and * operator can’t be converted to number?

Hey Annestezia,

did you come up with some steps towards the solution?

Hey Michael,

Nope, I decided to step back for now
Spotted a very beautiful solution here on forum and consider rewriting the whole thing as it’s too ugly at it’s core and I’m not smart enough for this, not yet

Dear people,

I’m still struggling with test #13 of js calculator, think I’ve come up with correct logic, but now React is in the way of my “happily ever after”.

Here’s my code

So there’s the ‘state-one-step-behind’ issue which doesnt let my fixInput function work as expected . As a solution for this I found that I should

explicitly pass the new input value as part of the event being raised

here
and here

They create the second handler, and run it inside the first one, I don’t understand how to use it in my app :confused:

Is the handler I got now outter one? what would the second handler look like? should I move fixInput function there? I’m blank

Please help me