How to Build a Simple Calculator Using HTML, CSS, and JavaScript | Code Skills

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.

Simple Calculator using HTML, CSS and JavaScript

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.

Folder structure of the calculator project

HTML Code

Open the index.html file and paste the following code:

Related: Steps to Understanding Basic HTML Code





Simple Calculator using HTML, CSS and JavaScript






































The structure of the calculator is created using the

tag. The

tag contains five rows which represent five horizontal sections of the calculator. Each row is created using the 

tag. Each

tag contains

tags (Table Data) which holds the display screen and buttons of the calculator.

Calculator Rows

CSS Code

Open the styles.css file and paste the following code:

Related: Simple CSS Code Examples You Can Learn in 10 Minutes

@import url('https://fonts.googleapis.com/css2?family=Orbitron&display=swap');
.calculator {
padding: 10px;
border-radius: 1em;
height: 380px;
width: 400px;
margin: auto;
background-color: #191b28;
box-shadow: rgba(0, 0, 0, 0.19) 0px 10px 20px, rgba(0, 0, 0, 0.23) 0px 6px 6px;
}
.display-box {
font-family: 'Orbitron', sans-serif;
background-color: #dcdbe1;
border: solid black 0.5px;
color: black;
border-radius: 5px;
width: 100%;
height: 65%;
}
.button {
font-family: 'Orbitron', sans-serif;
background-color: #64278f;
color: white;
border: solid black 0.5px;
width: 100%;
border-radius: 5px;
height: 70%;
outline: none;
}
.button:active {
background: #e5e5e5;
-webkit-box-shadow: inset 0px 0px 5px #c1c1c1;
-moz-box-shadow: inset 0px 0px 5px #c1c1c1;
box-shadow: inset 0px 0px 5px #c1c1c1;
}

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.

Related: How to Use CSS box-shadow: Tricks and Examples

JavaScript Code

Open the script.js file and paste the following code:

// This function clear all the values
function clearScreen() {
document.getElementById("result").value = "";
}

// This function display values
function display(value) {
document.getElementById("result").value += value;
}
// This function evaluates the expression and return result
function calculate() {
var p = document.getElementById("result").value;
var q = eval(p);
document.getElementById("result").value = q;
}

Understanding the JavaScript Code

The clearScreen()display(), and calculate() functions are used to 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.

function clearScreen() {
document.getElementById("result").value = "";
}

Displaying Values

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

function display(value) {
document.getElementById("result").value += value;
}

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.

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.

function calculate() {
var p = document.getElementById("result").value;
var q = eval(p);
document.getElementById("result").value = q;
}

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.

Related: Python Project Ideas Suitable for Beginners

Develop Cool Programming Projects

You 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

Post a Comment

Previous Post Next Post