Create Simple Basic Calculator Using JavaScript

Create Simple Basic Calculator Using JavaScript, Online Basic Calculator Using JavaScript

Hello friends, today I will tell you through this tutorial how to create a simple calculator through javascript program. I will tell you step by step about this.

Overview

Step 1: Create html file (simple-calculator.html).
Step 2: Create css file (simple-calculator.css).
Step 3: Create js file (simple-calculator.js).

Step 1: Create html file (simple-calculator.html).

 

simple-calculator.html

<html> 
<head> 
<title>Simple Basic Calculator With Exapmle</title>
<link rel="stylesheet" href="/simple-calculator.css">
</head> 
<body> 
<div class = title >Simple Basic Calculator With Exapmle</div> 
<table border="1"> 
<tr> 
<td colspan="3"><input type="text" id="result"/></td> 
<td><input type="button" value="Clear" onclick="clr()"/> </td> 
</tr> 
<tr> 
<td><input type="button" value="1" onclick="dis('1')"/> </td> 
<td><input type="button" value="2" onclick="dis('2')"/> </td> 
<td><input type="button" value="3" onclick="dis('3')"/> </td> 
<td><input type="button" value="/" onclick="dis('/')"/> </td> 
</tr> 
<tr> 
<td><input type="button" value="4" onclick="dis('4')"/> </td> 
<td><input type="button" value="5" onclick="dis('5')"/> </td> 
<td><input type="button" value="6" onclick="dis('6')"/> </td> 
<td><input type="button" value="-" onclick="dis('-')"/> </td> 
</tr> 
<tr> 
<td><input type="button" value="7" onclick="dis('7')"/> </td> 
<td><input type="button" value="8" onclick="dis('8')"/> </td> 
<td><input type="button" value="9" onclick="dis('9')"/> </td> 
<td><input type="button" value="+" onclick="dis('+')"/> </td> 
</tr> 
<tr> 
<td><input type="button" value="." onclick="dis('.')"/> </td> 
<td><input type="button" value="0" onclick="dis('0')"/> </td> 
<td><input type="button" value="=" onclick="solve()"/> </td> 
<td><input type="button" value="*" onclick="dis('*')"/> </td> 
</tr> 
</table> 
<script src="/simple-calculator.js" type="text/javascript"></script>
</body> 
</html>

 

Step 2: Create css file (simple-calculator.css).

 

simple-calculator.css

<style> 
.title{ 
margin-bottom: 10px; 
text-align:center; 
width: 210px; 
color:green; 
border: solid black 2px; 
}
input[type="button"] 
{ 
border: solid black 2px; 
width:100% 
}
input[type="text"] 
{ 
background-color:white; 
border: solid black 2px; 
width:100% 
} 
</style>

Step 3: Create js file (simple-calculator.js).

 

simple-calculator.js

<script> 
function dis(val) 
{ 
document.getElementById("result").value+=val 
} 
function solve() 
{ 
let x = document.getElementById("result").value 
let y = eval(x) 
document.getElementById("result").value = y 
} 
function clr() 
{ 
document.getElementById("result").value = "" 
} 
</script>

 

You can create this calculator by adding css and js code to html file.
You will have to type the code in this way. For this, you can see in the example below.

simple-calculator.html

<html> 
<head> 
<script> 
function dis(val) 
{ 
document.getElementById("result").value+=val 
} 
function solve() 
{ 
let x = document.getElementById("result").value 
let y = eval(x) 
document.getElementById("result").value = y 
} 
function clr() 
{ 
document.getElementById("result").value = "" 
} 
</script> 
<style> 
.title{ 
margin-bottom: 10px; 
text-align:center; 
width: 210px; 
color:green; 
border: solid black 2px; 
} 
input[type="button"] 
{ 
border: solid black 2px; 
width:100% 
} 
input[type="text"] 
{ 
background-color:white; 
border: solid black 2px; 
width:100% 
} 
</style> 
</head> 
<body> 
<div class = title >Simple Basic Calculator With Exapmle</div> 
<table border="1"> 
<tr> 
<td colspan="3"><input type="text" id="result"/></td> 
<td><input type="button" value="Clear" onclick="clr()"/> </td> 
</tr> 
<tr> 
<td><input type="button" value="1" onclick="dis('1')"/> </td> 
<td><input type="button" value="2" onclick="dis('2')"/> </td> 
<td><input type="button" value="3" onclick="dis('3')"/> </td> 
<td><input type="button" value="/" onclick="dis('/')"/> </td> 
</tr> 
<tr> 
<td><input type="button" value="4" onclick="dis('4')"/> </td> 
<td><input type="button" value="5" onclick="dis('5')"/> </td> 
<td><input type="button" value="6" onclick="dis('6')"/> </td> 
<td><input type="button" value="-" onclick="dis('-')"/> </td> 
</tr> 
<tr> 
<td><input type="button" value="7" onclick="dis('7')"/> </td> 
<td><input type="button" value="8" onclick="dis('8')"/> </td> 
<td><input type="button" value="9" onclick="dis('9')"/> </td> 
<td><input type="button" value="+" onclick="dis('+')"/> </td> 
</tr> 
<tr> 
<td><input type="button" value="." onclick="dis('.')"/> </td> 
<td><input type="button" value="0" onclick="dis('0')"/> </td> 
<td><input type="button" value="=" onclick="solve()"/> </td> 
<td><input type="button" value="*" onclick="dis('*')"/> </td> 
</tr> 
</table> 
</body> 
</html>