You can make a simple calculator using just core web technologies: HTML, CSS, and JavaScript. This calculator can perform basic mathematical operations like addition, subtraction, multiplication, and division.

Features of the Calculator

In this project, you are going to develop a calculator that will have the following features:

It will perform basic arithmetic operations like addition, subtraction, division, and multiplication. It will perform decimal operations. The calculator will display Infinity if you try to divide any number by zero. It will not display any result in case of invalid expression. For example, 5++9 will not display anything. Clear screen feature to clear the display screen anytime you want.

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 clears 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 the 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 HTML code for the calculator:

This project uses a

tag to create the overall structure of the calculator. The
tag contains five rows which represent five horizontal sections of the calculator. Each row has a correspondingtag. Eachtag contains
tags which hold the display screen and buttons of the calculator.

CSS Code

Open the styles.css file and paste the following CSS code for the calculator:

The above CSS styles the calculator. The .class selector in CSS selects elements with a specific class attribute. The .calculator and .display-box class selectors style the table structure and the display screen of the calculator respectively. @import imports the Orbitron font-family from Google fonts.

JavaScript Code

Open the script.js file and add functionality to the simple calculator using the following JavaScript code:

Understanding the JavaScript Code

The clearScreen(), display(), and calculate() functions add functionality to the Calculator.

Clearing Values

The clearScreen() function access the DOM using the id of the result and clear its value by assigning it an empty string. You can use DOM selectors to target various components of a page.

Displaying Values

The display() function accesses the DOM using the id of the result and appends the value of the clicked button to the result.

Evaluating Expression

The 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.

Develop Cool Programming Projects

You can improve your coding skills by developing projects, whether you’re a beginner or you’re getting back into coding after some time off. Creating fully-working apps, even simple ones, can boost your confidence.

You can try out many simple projects from games (chess, tic-tac-toe, Rock Paper Scissors) to simple utilities (to-do list, weight conversion, countdown clock).

Get your hands dirty with these projects and become a better developer.