Javascript calculator...can't implement switching operators

TODO for after you get the core functionality down: I noticed your Calculator.render() method was really redundant. Except for the last button you define, #clear, you are only changing two pieces of data, and using one of those twice. If you stored that data in an array, you could use Array.prototype.map() to DRY up your code. There was a lesson on using array mapping in React in the FCC curriculum

Since the only place you use the regexes is in handleInput(), you should move them there, and switch from var to const.

I’m running your code on Firefox, and copying it to Codepen, with which I’m more comfortable, and I’m getting a syntax error re: invalid regexp on both that and CodeSandbox. The only regex which potentially had a problem was the one with a lookbehind (not a standard JS engine feature). So, I replaced it with a pattern that tests for 00 after an operator, which I made for you in regexr. (If you don’t know about regexr, it’s past time). Once I did that, it rendered.

I also noticed that in your HTML, your #root div was not INSIDE the body, but above it. Is that causing problems for you re: the testing suite? I know it renders to the body element, so you can’t tell React to directly render to the body.

You are only shy 2 tests from passing (10 & 11). You can do this.

A huge thing I’m not seeing is any debug statements. Unless CodeSandbox has a debugger I don’t know about, console.log() is a lifesaver. As a matter of course, the top line of my projects is always: console.clear(), and I only get rid of it once they are polished to a gleam. Copy/paste this code over the first two lines of your inputhandler, open the console (F12 key), enter some input,and see if you can figure out what I have been saying about you TESTING THE WRONG STRING (inputString):

  handleInput(event) {
    console.log("entered input handler");
    console.log("this.state.inputString:", this.state.inputString);
    console.log("event.target.value:", event.target.value);
    if (!regex.test(this.state.inputString)) {
1 Like