Stuck on 2 bugs

Hello! :wave:

Apparently I got stuck on test #13 and #14.

I can’t seem to figure out the logic of letting users input 2 consecutive operators while preventing them to spam the operators. The logic I always come up is they will be able to spam the operators. To avoid confusion in the code, here’s where I’m working on:

handleOps = (e) => {

    const { display } = this.state;
    const { innerText } = e.target;
    const lastPressed = display.slice(-2).trim();
    
    if (!ops.includes(lastPressed)) {
      this.setState({
        display: `${display} ${innerText} `
      })
    }
    
  }

Please help me on the last bug also! Thank you for your time and effort!

Hi @ xynoan

I don’t think I’m the best person to help you with this - I’m still getting my head around React and I haven’t even started this project.
But I’ll have a go anyway. :smile:

It looks as if your code only accepts the first of two (or more) consecutive operators.
But, from reading the instructions, that seems like the opposite of what’s required.

From my understanding of the instructions, if the user enters two (or more) consecutive operators, then only the last (most recent) operator that’s entered should be used.
That also seems to be how the example app works on CodePen.

So, I think that, if the user enters consecutive operators, then your code should replace lastPressed with the new operator.

Obviously, the minus operator is an exception.

Based on the way that the example app works:

  • an operator followed by the minus sign followed by a number → all OK
  • an operator followed by the minus sign followed by an operator → just use the last operator (i.e. discard the previous operator and the minus sign).

I hope some of that makes sense.

1 Like

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