Javascript (React) Calculator Feedback

Hi all,

I’ve recently “completed” my calculator project. I put completed in quotes because I’m very much aware that my code needs to be cleaned up and simplified in spots. My process (for better or worse) is typically to get things working first, and then go back to reevaluate how I wrote the code and look for what I can do better.

Anyway, my main request right now is to play around with the actual calculator and let me know if you find any bugs. You can use your keyboard for all the controls as well, though there may be a couple issues there depending on your keyboard layout. Also, if there’s any LOST fans out there, I added an Easter egg (think numbers on the show). Here’s the link:

Thanks!

looks good @joelac32 - I found a few bugs…

  • try typing this… number operator decimal operator
  • you can put in more numbers than the display can fit - try typing 20 numbers
  • if you put in 9/0 you get a string “infinity”, you can then put in a number after it (this one probably isn’t much of a problem, but it’s there)

I took a quick look at the code to try and find your easter egg (found it), one thing I saw at first glance when you are setting state is that you can “condense” this…

        this.setState({display: this.state.display+x});
        this.setState({decimal: true});
        this.setState({zeroAllow: true});

into this…

        this.setState({
          display: this.state.display+x,
          decimal: true,
          zeroAllow: true
        });

I guess they both work, but I think the second way is more standard

Steps:

  • Try inserting 5 / 2
  • Hit Enter
  • Hit the decimal button
  • Hit another number (e.g. 9)

Result: you end up with 2.5.9

*You might want to check up on your logic for whether the user can add another decimal or if the input should be ignored.

Nice job!

Thank you! I appreciate having someone check possibilities I didn’t even think to check! And yes, I was hesitant to combine the setState objects, but that was one of the things I wanted to look back and to try and improve. Thanks for the tip!

Going to work on fixing bugs now!

Thanks! I never thought about that… I’ll work on a solution!