Devlog - Simple Calculator by @princekumar
What I built:
A simple web calculator. It can do +, -, x, ÷, percentage and has AC, backspace and a copy button. I made it with just HTML, CSS and JS. Link: http://princekumar189344-netizen.github.io/Simple-Calculator/
Why I built it:
I wanted to make something useful that I use daily. Also I was tired of making only games, so I thought a calculator would be a good small project to understand DOM manipulation properly. I started with just basic buttons and then added extra features.
How I built it:
The HTML is just a div with an input as display and buttons in a grid. In CSS I used display: grid with 4 columns for the buttons. I gave different colors - red for AC/backspace, yellow for operators, green for copy.
In JS, the logic is very simple:
- appendValue() adds numbers/symbols to the display value.
- clearDisplay() clears everything.
- backspace() uses slice(0, -1) to remove last char.
- For calculate(), I had a problem because my buttons show ÷ and × but JS needs / and *. So I used .replace(/÷/g, “/”) before using eval(). I know eval is not best practice but for this small project it was easiest.
- percentage() just divides by 100.
- copyResult() uses navigator.clipboard.writeText and shows an alert.
Challenges:
- The display was showing 79266-9 type values when I clicked operators fast, I fixed it by making sure values just append.
- Copy button was not working on http, it needs https, but GitHub pages is https so it works now.
- I first used a lot of nth-child in CSS which became confusing, next time I will give classes to buttons.
What I learned:
How grid works, how to handle button clicks without making 20 functions, and how clipboard API works. Also learned that readonly input is better than div for display.