This is a Progressive Web App (PWA) for a simple calculator built using vanilla JavaScript. It includes several advanced features:
- Offline Functionality: Operates seamlessly offline, leveraging Service Workers for caching.
- Auto Completion: easily complete long expressions using auto complete for brackets
- Calculation History: Tracks and displays a history of past calculations.
- Dark and Light Mode: Provides a toggle between dark and light themes for enhanced usability in different lighting conditions.
The app is designed to be fast, responsive, and user-friendly, making it a versatile tool for quick calculations on the go.
Experience the demo, by clicking here
laptop.mp4
ipad.mp4
iphone.mp4
The calculator implements a parser and evaluater built from scratch. Hence, this section is aimed at giving an overview of the parser and evaluator.
- Perform translation
- Perform Pre checks
- Remove all syntax sugars
- Perform Post checks
- Make the data structure by analyzing characters left to right and inside-out
- Iteratively computing the DS with a depth first approach
- changes the symbol of multiplication and division to simple form ✅
- Removes any commas ✅
- check that
%operator is not followed by a number ✅
- Exact string
.represents0 +-becomes-✅- a bracket right after a number has implicit multiplication ✅
---..becomes+or-depending on count and+++...becomes+✅- Add implicit
0before+and-if a number is missing ✅ %becomes*0.01called as expanding the percent symbol ✅add brackets to follow BODMAS rule(indstead modified evaluation rules) ✅decimal point should have numbers on both side(js can handle vague decimal cases) ✅
- check that each operator has two operands except (
%should be followed by*) ✅ - check that no two operators are consecutive ✅ Solved by the first check
The evaluator is quite complex but it does the following steps in the given order
- Generate an intermediate data structure to represent the expression. This data structure is comprises of various data types such as numbers, strings for operators and nested arrays for brackets
- The evaluator iteratively evaluates the the expression from left to right for each operator as specified by the BODMAS rule.
- Whenever a Array(brackets) is encountered, it recursively first evaluates the sub expression insider the array(brackets)
- The intermediate data structure is worked on repeatedly until it contains only a single number, the answer or is error.
I highly recommend checking out the following resources:
- favicon Flaticon-keys
- Add Commas according to international system of counting only for visual component
Test Fetures and styling on different devicesFix Error and Nan situationsFix Solving algorithmPolish the PWA FeaturesHandle case where many digits are displayedAdd 2 themes only, dark and light [partially done]Refactor CodeDecrease font size for output if large number needs to be displayedLast number should not be displayed on the screensingle decimal point gives Nan- ~~Add syntax sugar for
+as--~~ Fix Styling for History tabMake the app a PWAAdd more themesScraped for only two themesAdd Keyboard Support for the calculatorAdd dark mode option for background onlyclicking on a history cell copies the answer to clipboardclicking equals and then calculating should copy the value from previous answerAdd logic to pick random color pallete from group of palletesCSS polishingCopy the latest answer to clipboard when equals is clickedAdd togglable Dark ThemeScrappedDesign portrait friendly css as part of Mobile-first designadd JS to deal with history tab in different orientationsDesign Landscape cssUse decimal.js for floating point arithmeticdebug the issue of (1)-(1)Add cursor, so that the user can edit mistakes inbetween the expressionScrappedFormula in the history tab should have space beside operators for better visibilityrecheck the evaluation rule of%Add Historyadding opening bracket at the start leads to NaNAdd % and power operatorsAdd Auto Compute featureCSS pendingAdd Syntax SugarsAdd checksAdd Floating Point numbersAdd modulo operators