To design the basic calculator, we will use HTML, CSS, and JavaScript. HTML is used to design the basic structure of the calculator. CSS styles are used to apply styles on the calculator and JavaScript is used to add the calculation functionality.
Approach:
Created the design with the HTML Table where firstis holding the input field with id=”result” and the rest are filled with input button.
With every click of the button, it displays the respective value of the button to the input field by using the function dis().
myFunction() is used to set the value pressed from the keyboard to the same input field.
Calculation of the numbers can be done by both the “Enter” key as well as the “=” button on the Calculator UI. The solve() function is evaluating the result with the math.evaluate() and display the final answer to the input field with id=”result”.
clr() function is also defined to clear the input field.
Example:
<html>
<head>
<<0 <1<2
<3
<4<5<2
<7
<4<9<2html1
<4html3<2html5html6<0>
<<0 <1<2
>4
<4<5<2
>8
<4<9<2html1
<4html3<2html5html6<0>
<1
<<4>
<4<7
<8<9
<8head1
<8head3
<4head5
<4head8
<8>0
<8>2
<8>4
<8>6
<8>8
<80
<82
<84
<4head5
<49
<8<1
<8>8
<80
<82
<84
<8<01
<4head5
<05<4>
<05head>
<11
<<14>
<<18 <19<2<21>
<4<<25>
<8<<29<33<34 <35<2<53 <54<2<23 <57<2<26
Readers like you help support MUO. When you make a purchase using links on our site, we may earn an affiliate commission. Read More.
The best way to learn JavaScript is to build projects. If you want to become a good web developer, you need to start creating projects as soon as possible. You can start by building beginner-level projects like a simple calculator, digital clock, or stopwatch.
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.
The code used in this project is available in a GitHub repository and is free for you to use under the MIT license. If you want to have a look at a live version of this project, you can check out this demo.
Components of the Calculator
The calculator consists of the following components:
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:
html> <htmllang="en"dir="ltr">
<head> <metacharset="utf-8"> <title>Simple Calculator using HTML, CSS and JavaScripttitle> <linkrel="stylesheet"href="styles.css"> head>
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:
// This function clear all the values functionclearScreen() { document.getElementById("result").value = ""; }
// This function display values functiondisplay(value) { document.getElementById("result").value += value; }
// This function evaluates the expression and returns result functioncalculate() { 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 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.
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.
The JavaScript eval() function evaluates an expression that you pass to it. It returns the result of that expression.
functioncalculate() { var p = document.getElementById("result").value; var q = eval(p); document.getElementById("result").value = q; }
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).
How to create a calculator in JavaScript?
A Guide to Build a Simple Calculator.
Initial HTML Code to Include the . js and . css files. ... .
Input the Digits And Operators. This step creates buttons for digits from 0 to 9 and operators like +,-,*,/, and = ... .
Add the Styling In the . css file. To beautify what you see on the screen, styling is crucial..
How to calculate using JavaScript?
Converting to number data types.
let myNumber = "74"; myNumber += 3; You end up with the result 743, not 77, because myNumber is actually defined as a string. ... .
typeof myNumber; To fix the calculation, you can do this:.
let myNumber = "74"; myNumber = Number(myNumber) + 3; The result is then 77, as initially expected..
How to make a calculator using JavaScript and HTML?
Result.html
const number1 = parseFloat(prompt ('Enter the first number: ')); const number2 = parseFloat(prompt ('Enter the second number: ')); let result; // declaration of the variable. // use if, elseif and else keyword to define the calculator condition in JavaScript.
How to make a calculator in JavaScript without eval?