See the live version of Browser Calculator project.
Project made during learning process with my mentor program devmentor.pl. The project is a simple browser calculator implemented in JavaScript, which allows users to perform basic arithmetic operations such as addition, subtraction, multiplication, division, and exponentiation. The calculator also keeps a history of all operations performed, which can be displayed to the user. The program prompts the user for input, validates the operations, and handles errors such as division by zero or invalid inputs. The main functionalities include performing calculations, storing operation history, and providing user feedback.
During the refactoring process, the code was optimized to improve readability and reduce redundancy. The main changes included:
Replacing multiple if-else statements with a dictionary (operations): This allowed for a more concise and maintainable way to map operations to their corresponding methods.
Binding methods to the Calculator instance: This ensured that the correct context (this) was maintained when calling the methods from the operations dictionary.
Simplifying the user input validation: The code now directly checks if the action is valid and if the inputs are numbers, reducing the complexity of the logic.
Are you interested in SCSS and JavaScript? See my other project Landing Page MGUU.
No Required
The refactoring made the code more modular and easier to extend. In the future, additional operations could be added by simply extending the operations dictionary and adding corresponding methods to the Calculator class. Furthermore, the code could be further improved by adding more robust error handling and possibly a user interface for a better user experience.
If you have questions, ideas, or just want to chat about code (or the meaning of life), don’t hesitate to contact me. Open an issue, drop me a pull request, or send a message—carrier pigeon works too, but GitHub might be faster. Let’s build something awesome together! 🚀
Thanks to my Mentor - devmentor.pl – for providing me with this task and for code review.