Today, I wanted to mess around with something I’ve been calling “calculator bricks.” It’s basically building a simple calculator, but I wanted to do it in a way that felt like assembling building blocks.
Getting Started
First, I grabbed a fresh sheet of paper and a pen. No fancy tools, just good old-fashioned planning. I started by jotting down the basic operations I wanted: addition, subtraction, multiplication, and division.


Then, I thought about how a user would interact with it. I needed buttons for numbers (0-9), the operations (+, -, , /), an equals button (=), and a clear button (C). I sketched out a rough layout, kind of like those old-school calculators.
Building the Logic
This is where it got interesting. I decided to use plain old HTML and JavaScript, nothing too fancy. I created a simple HTML structure with a div
for the display and a bunch of button
elements for the inputs. I used onclick
for javascript.
- I made a variable to store the current number being entered.
- Another variable to store the previous number.
- And one to keep track of the selected operation.
Putting It All Together
I slowly built out the JavaScript functions. Each number button appended its value to the current number string. The operation buttons stored the current number and the operation type.
The equals button was the trickiest. It had to perform the actual calculation based on the stored numbers and operation. I used a switch
statement to handle the different operations. A bunch of if
statements could have worked too, but the switch
felt cleaner.
The Finishing Touches
Finally, I added a clear button function to reset everything. I tested it, found some bugs, and fixed them. It wasn’t perfect, but it worked! I could enter numbers, perform calculations, and clear the display.
It was a fun little project. It reminded me that even complex things can be broken down into smaller, manageable “bricks.” And sometimes, the simplest tools are the best for getting the job done.

