5 * - 5 = -25 |OR| 5 * - + 5 = 10, but not both at the same time #13

Passing 15 out of 16
Stuck on number 13
I can get 5 * - 5 to evaluate to -25 or
5 * - + 5 to equal 10
but not both at the same time
anything I come up with feels really hacky and would most likely break other more normal operations.

Passing 15 out of 16, for the Calculator at FreeCodeCamp.com

repo
live demo, be sure to use the NavBar to get to Calculator
use the hamburger menu on the top left and select JavaScript Calculator to run the test suite, they say it’s designed for Chrome and may encounter bugs in other browsers.

Calculator.js

  handleClick = (buttonName) => {
    let { displayNumber, operatorFlag, decimalFlag } = this.state;
    // let lastChar = displayNumber[displayNumber.length - 1];
    switch (true) {
      // 24:00
      case buttonName === "0" ||
        buttonName === "1" ||
        buttonName === "2" ||
        buttonName === "3" ||
        buttonName === "4" ||
        buttonName === "5" ||
        buttonName === "6" ||
        buttonName === "7" ||
        buttonName === "8" ||
        buttonName === "9":
        if (displayNumber !== "0") {
          displayNumber += buttonName;
          operatorFlag = false;
        } else {
          displayNumber = buttonName;
        }
        break;
      case buttonName === "-":
        if (!operatorFlag) {
          displayNumber += buttonName;
          operatorFlag = true;
          this.setState({ decimalFlag: false });
        } else {
          // const newNumber = displayNumber.slice(0, displayNumber.length - 1);
          // displayNumber = newNumber;
          displayNumber += buttonName;
          operatorFlag = true;
          this.setState({ decimalFlag: false });
        }
        break;
      //25:00
      case buttonName === "+" ||
        buttonName === "-" ||
        buttonName === "*" ||
        buttonName === "/":
        if (!operatorFlag) {
          displayNumber += buttonName;
          operatorFlag = true;
          this.setState({ decimalFlag: false });
        } else {
          const newNumber = displayNumber.slice(0, displayNumber.length - 1);
          displayNumber = newNumber;
          displayNumber += buttonName;
        }
        break;
      //33:00 clear
      case buttonName === "C":
        displayNumber = "0";
        operatorFlag = false;
        this.setState({ decimalFlag: false });
        break;
      //35:00
      case buttonName === "=":
        displayNumber = evaluate(displayNumber);
        operatorFlag = false;
        this.setState({ decimalFlag: true });
        break;
      //38:00
      case buttonName === ".":
        if (!decimalFlag) {
          displayNumber += ".";
          this.setState({ decimalFlag: true });
        }
        break;
      default:
    }
    this.setState({ operatorFlag });
    this.setState({ displayNumber });
  };
1 Like

I see 16/16 passed tests and tried both 5 * -5 and 5 * - + 5 manually and get the correct answers.

Looks like you have worked through this and got it. Nice!

2 Likes

Yeah, I finally got it to pass at 2 am, but it feels more like a hack. I’ve used math to solve word problems, but this is the first time I’ve used regular expressions to brute force a math problem. Thanks for checking it out.

1 Like

If you want to try and implement it in a more algorithmic fashion, look up the Shunting Yard Algorithm and, if you feel like doing so, take a crack at implementing it for your calculator.

1 Like

And why are you using switch (true)? Yes, there are some use cases for it, but it seems that the standard switch (buttonName) would be more concise. It also just seems more semantically “correct”.

2 Likes

yes, thank you. I knew I had to be overlooking something. I mean it passes the test, but not the spirit. I just focused on getting by in one unique use case, so I knew my understanding wasn’t complete.

That’s a good callout. I just jumped to the live demo and tried it, not paying close attention to the code provided here.

@TurtleWolf you should review switch as it does seem like there is a misunderstanding of it’s proper usage. If you have questions regarding it, do ask.

1 Like

good to know. I was actually unsure about that, but it’s what was recommended in one of the tutorials I went through. The last time I used a switch I didn’t put anything in it, I just called switch() and it seemed to work, so I wasn’t sure what true was referring to here either… but knowing I can pass buttonName into it does seem more practical, could you elaborate on that or point me to a link to fill in the understanding?

yes, I think I just did ask… I wasn’t sure about that either. I got it from one of the tutorials I was watching, but I was confused by how he was calling it too

I’d recommend reviewing the four lessons starting at Selecting from many options with switch statements

After reviewing those, if you still need help understanding or have specific questions feel free to ask.

1 Like

I mean, it works just fine. I mean, I can use a standard screwdriver to screw in a phillips head, it’s just not the right approach. Using the fall-through approach would just be cleaner.

Right, there are cases where it makes sense, usually if you have different types of logic in different cases. But in this case, where all you’re doing is checking a single value, the simple use of switch makes more sense. You can stack those different cases with fall-through.

The issue is that your approach is a little messy, and to me at least, the switch (true) implies that there is come more complex logic going on in the cases, causing a comprehensional speed bump when I can’t find them.

2 Likes