Please help me get the final challenge on the Calculator Project

Please help me get the final challenge on the Calculator Project
0

I cannot figure out how to pass this challenge:

13. If 2 or more operators are entered consecutively, the operation performed should be the last operator entered (excluding the negative (-) sign.

The

Here is the method that is controlling the output. Note that the else if statement !endsWithNegativeSign.test(formula) is where near where this issue lies.

getOperator(e) {
      const endsWithOperator = /[x+‑/]$/,
      endsWithNegativeSign = /[x/+]‑$/;
      if (!this.state.curValue.includes('Digit')) {
        const value = e.target.value;
        const { formula, preValue, evaluated } = this.state;
        this.setState({ curValue: value, evaluated: false });
        if (evaluated) {
          this.setState({ formula: preValue + value });
        } else if (!endsWithOperator.test(formula)) {
          this.setState({
            prevVal: formula,
            formula: formula + value
          });
        } else if (!endsWithNegativeSign.test(formula)) {
          this.setState({
            formula: value + (endsWithNegativeSign.test(formula + value)
              ? formula : preValue) + value // This is the line controlling output.
          });
        } else if (value !== '‑') {
          this.setState({
            formula: preValue + value
          });
        }
      }
    }

This is the evaluate method as well, which could also be part of the problem.

    evaluate() {
         if (!this.state.curValue.includes(`Digit`) && !this.state.evaluated && !endsWithNegativeSign.test(this.state.formula)) {
           
            let exp = this.state.formula;
            while (endsWithOperator.test(exp)) {
                exp = exp.slice(0, -1);
            }
            exp = exp.replace(/x/g, `*`).replace(/-/g, '-');
            let ans = eval(exp);
            if (ans){
            this.setState({
                curValue: ans.toString(),
                formula: exp.replace(/\*/g, '⋅').replace(/-/g, '‑') + '=' + ans,
                preValue: ans,
                evaluated: true
            })};
        }
    }

Here is a link to the Demo.
And here is the Repository.

Any help would be appreciated on this. Thanks in advance!

1 Like

Hello,

The issue to me appears to be in the logic that occurs when you press two consecutive operator signs. The output clears not only the first operator but also the digit prior to that. ie:

5++5= the second + clears the first + as well as the initial 5 leaving only +5= which results to 5.
edit: When the second operator is clicked it actually resets the formula to +0+ which I think is due to the preVal and formula being off in this code block (see my two comments below):

getOperator(e) {
      if (!this.state.curValue.includes('Digit')) {
        const value = e.target.value;
        const { formula, preValue, evaluated } = this.state;
        this.setState({ curValue: value, evaluated: false });
        if (evaluated) {
          this.setState({ formula: preValue + value });
        } else if (!endsWithOperator.test(formula)) {
          this.setState({
            prevVal: formula, //Is this variable named correctly?
            formula: formula + value
          });
        } else if (!endsWithNegativeSign.test(formula)) {
          this.setState({
            formula: value + (endsWithNegativeSign.test(formula + value)
              ? formula : preValue) + value
          });
        } else if (value !== '‑') {
          this.setState({
            formula: preValue + value // Is preValue defined here or still "0"?
          });
        }
      }
    }

I’m working through your code but it’s slow going as I’m not familiar with GitHub. Well past time for me to learn. I’ll update if I see anything specific.
-J

Thanks for looking at it for me. I was able to sort it out and added in some extra functionality so that you can use a keyboard input as well. I am not 100% in love with the style of the output but it’s passing the tests now :slight_smile: You were on the right track with the variable mix up. I didn’t get around to looking at it until today though.

https://mnichols08-calculator.netlify.com/

1 Like