Issue with test 13 in JavaScript Calculator project

Hello everyone, hope everyone is great.

I’m getting a strange error in test 13, which I tried Googling but I still don’t understand what is it about, and is keeping me from advancing on the curriculum.

Error is:

Uncaught SyntaxError: Invalid left-hand side expression in postfix operation

Link to Calc is:

Appreciate any feedback or guidance in the correct direction.


This error is in reference to shorthand syntax we often do to increment/decrement a variable. e.g. x++ or x--

I have not found the exact issue in your code, but it would revolve around how you store the data to pass into eval(). A hypothetical situation of inputting '24*+4' would be nonsense to eval() and prob trigger an error akin to the one you see.

Hey! Thank you very much for your response. That does make sense in term of the error, what I don’t understand is why my Regex is not matching and replacing the operators when there are more than 1 together.
I’m currently using this:

const twoOrMoreOperators = new RegExp(/[-+*/]{2,}/gm); //Declaring Regex

//This gets executed when the '=' is clicked
if (this.state.twoOrMoreOps && this.state.lastOperator != '-'){
      const newFormula = this.state.formula.replace(twoOrMoreOperators, this.state.lastOperator) + this.state.input
        formula: eval(newFormula),
        output: eval(newFormula),
        input: ''

Which I tested in and works perfectly, so I’m not sure why the replacement is not being done correctly. In your example if I have x++, it should replace ‘++’ by the last operator which is stored in the state of the app.

Any suggestions?

Thanks again for your help!

Hey, a cursory look at your new RegExp() makes me wonder if that is indeed how you are supposed to pass in the initial arguments to the constructor.

I seem to remember the syntax for a regex like /[a-z]/i for example, would be:
const reg = new RegExp(/[a-z]/, i);

The other potential issue I see is the unescaped backslash / within your regex, (the division operator) it is likely that JS at runtime will treat that backslash as the end of your regex. In other words, you want JS to see /[-+*/]{2,}/, but JS might be seeing /[-+*/

I usually use the literal notation for regex and not explicitly construct a RegEx object, so my ideas may be off :slight_smile: I tried looking in the docs a bit but it seems like I should be right.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.