I Made A JavaScript Calculator!

Happy New Year!

This new year has gotten off to a pretty good start for me. I have finished my JavaScript calculator, which can be viewed live on CodePen here.

The user stories I had to fufill to complete the challenge:

  1. Add, subtract, multipy and divide two numbers.
  2. Clear the input field with a clear button.
  3. Chain mathematical operations together until hitting the equal button, and still get the correct output.

I realized from working on my Pomodoro clock that I sometimes get caught up with “making it pretty” that I was losing focus on making it work. So, once I created my basic buttons and display, I left the styling bare to finish later. I feel like this worked to my advantage in keeping me focused on one task at a time.

When it came to making my buttons functional, I knew that I would need to store the keys being clicked by the user as well as make them display on the screen. It actually didn’t take long for me to get that part working. The hard part was really getting the calculator to evaluate the input. I started off trying to use methods to take in the input as one long string, converting it to integers and operators, then evaluating them. I was spinning my wheels for a bit.

After consulting the Google gods, I discovered that there is actually a method called ‘eval’ that can simply evaluate a string as an equation. Bingo! So far, I haven’t been able to break it and I haven’t gotten word that anyone else has broken it yet. Sounds like a success to me!

I used Palleton to find a triad of pastel colors and a couple Google Fonts to give my calculator some life.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s