Building a Calculator…

Richard Agyemang
4 min readMay 4, 2021
Photo by Editors Keys on Unsplash

We all use calculators without giving much thought as to how the calculators keep spitting out numbers. To some of us, it’s just one of those tools that help us get things done faster and easier. As part of the Odin projects assignments, I was tasked to create a calculator. I thought to myself “oh just a calculator”. It turned out building something as easy as a calculator from a user's point of view differs enormously from the standpoint of a developer. You literally have to think about every step and process a user may take in using your application.

As usual, I always try to spell out the problems I encountered during the execution of this program. This time I was complacent so I decided not to rewrite the problems and internalize them for myself. A huge Mistake!!. I paid in time and frustration.

I had to go back and rewrite the whole code. To be precise, I had to make sense of what I was writing and I have come to understand that you may want to reach out for help but the problem is the ability to actually know the right questions to ask. I think a way of asking the relevant question but not just any question is having a deep understanding of the problem you’re trying to solve. It won’t be enough just to have a cursory idea of the problem you’re trying to solve. I noticed I was going back and forth to reread and rewrite the whole program because I was only taking pieces of information as I code. I used to think it is easier for me to just code in pieces of code step by step but actually, it is better to make plans for the whole problem set then break down the code needed to accomplish those functionalities you would want to incorporate into your code. One key discovery in my learning is that it is mostly not about writing just codes or syntax but a critical understanding and analysis of problem sets. Most of the time we just jumped into code as a beginner programmer but that is absolutely wrong at least in my opinion. I found out In building this calculator, I was just following the steps laid out by TOP. No doubt this is great but at the same time, it might deprive you of the opportunity to plan and internalize the problems you’re trying to solve. It would be much better if you can personally internalize the problem and using their directions or pointers as an inspiration. Developing this calculator has given me mad respect for all programmers out there. It is quite challenging just to make something as simple as a calculator work. The thought process is not linear rather a battle to make sense of what you’re trying to write.

I started by adding an event listener to the whole document. Soon I realized that when I wanted an event that is outside my document to execute It didn’t seem to work very well. So I put the piece of code in my document but it turned out It was loading very slow. What I did was to select all the buttons after which I looped through all the individual button elements so that if each button is clicked I can initiate the task I would like it to perform.

I think the best way to deal with the DOM is that if you want to work with a group or collection of elements your best bet is to select all the elements then loop over them after which you apply the individual functionalities that you want to see in that particular element you want. This way, your elements are alert and waiting for your commands to execute. This is completely different if you’re just applying individual event handler to each element

One more thing I don’t see a lot of experienced programmers talk about is the art of debugging your code. This I think is the single most important task every programmer should have at their fingertips. I realized it is easy to write the initial code but finding and removing bugs is a total hell. I’m thinking of sharpening my debugging skills after this project.

Sometimes it is really tempting to copy pieces of codes from the internet without actually understanding them. It sounds easy and once you copy the code you can just move on to work on another task. If you feel this way kindly don’t. I see it as a deferred problem in which you may meet the same problem in coming projects you might want to do. What I found that works really well for me is taking that piece of code and playing around with it on replit just to have a feel of how tweaking other parts of the code will affect the program. This way you gain valuable insights into the code and you may be able to rewrite that code in your own way.

I have also noticed there are days you just write codes that do not make sense and there are other days, you might feel like your mind is a totally different creature that spits out beautiful codes. So no matter the state of your mind on a particular day just keep grinding. I feel deeply satisfied I was able to do this. In fact, this is the best way to learn anything. I write this every time and I will continue to do so. Odin project is the best.

ProjectLink: https://github.com/owuracoder/calculator.git

--

--