The best way to learn JavaScript is to build projects. If you want to become a good web developer, you need to start creating as soon as possible. You can start by building beginner-level projects like a simple calculator, digital clock, stopwatch, etc.
In this article, you’ll learn how to make a simple calculator using HTML, CSS, and JavaScript. This calculator can perform basic mathematical operations like addition, subtraction, multiplication, and division.
Let’s get started.
Components of the Calculator
The calculator consists of the following components:
Mathematical Operators: Addition (+), Subtraction (-), Multiplication (*), and Division (/).
Digits and Decimal Button: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, . .
Display Screen: It displays the mathematical expression and the result.
Clear Screen Button: It clear all mathematical values.
Calculate button (=): It evaluates the mathematical expression and returns the result.
Folder Structure of the Calculator Project
Create a root folder that contains the HTML, CSS, and JavaScript files. You can name the files anything you want. Here the root folder is named Calculator. According to standard naming convention, the HTML, CSS, and JavaScript files are named index.html, styles.css, and script.js respectively.
HTML Code
Open the index.html file and paste the following code:
Simple Calculator using HTML, CSS and JavaScript
The structure of the calculator is created using the
tags (Table Data) which holds the display screen and buttons of the calculator. CSS CodeOpen the styles.css file and paste the following code:
The above CSS is used to style the calculator. The .class selector is used for selecting elements with a specific class attribute. The .calculator, .display-box, and .button class selectors are used to style table structure, the display screen, and buttons of the calculator respectively. @import is used to import the Orbitron font-family from Google fonts.
JavaScript CodeOpen the script.js file and paste the following code:
Understanding the JavaScript CodeThe clearScreen(), display(), and calculate() functions are used to add functionality to the Calculator. Clearing ValuesThe clearScreen() function access the DOM using the id of the result and clear its value by assigning it an empty string.
Displaying Valuesdisplay() function accesses the DOM using the id of the result and appends the value of the clicked button to the result.
Evaluating ExpressionThe calculate() function accesses the DOM using the id of the result and evaluates the expression using the eval() function. The evaluated value of the expression is again assigned to the result. Note: The eval() function is used in JavaScript to evaluate the expression passed to it as a parameter. It returns the evaluated result of the mathematical expression.
If you want to have a look at the complete source code used in this article, here’s the GitHub repository. Also, if you want to have a look at the live version of this project, you can check it out through GitHub as well.
Develop Cool Programming ProjectsYou can raise your engineering skills to the next level by developing projects. Whether you’re a beginner or you’re getting back into coding after some time off, developing projects can be a big help in boosting your confidence. You can try out many simple projects like a Chess Game, To-Do List App, Weight Conversion Tool, Tic Tac Toe game, Rock Paper Scissors game, Web Scraper with Python, Login System, Chatbot, Snake game, Tetris game, Tip Calculator, Countdown Clock, URL Shortener, etc. Get your hands dirty with these projects and become a better developer.
Read Next
About The Author
How to Build a Simple Calculator Using HTML, CSS, and JavaScript |