# JavaScript - Simple Basic Calculator

In this tutorial we will create a Simple Basic Calculator using JavaScript. This code automatically generate a calculator app when the user run the web application. The code itself use onclick() function to initiate a several method by means of computing a number when you pass a value as parameter.. You can apply this script to your code, it is a user-friendly program feel free to modify it.

We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer.
.

## Getting Started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

## The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.

`<!DOCTYPE html><html lang="en">	<head>		<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>		<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>	</head><body>	<nav class="navbar navbar-default">		<div class="container-fluid">			<a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>		</div>	</nav>	<div class="col-md-3"></div>	<div class="col-md-6 well">		<h3 class="text-primary">JavaScript - Simple Basic Calculator</h3>		<hr style="border-top:1px dotted #ccc;"/>			<div class="col-md-6 well" style="background-color:green;">			<form name="calculator">				<input type="text" id="result" style="width:100%; height:50px; font-size:30px;">				<br /><br />				<button type="button" onclick="calculate('1')" style="padding:25px; margin-left:10px; font-size:14px;">1</button>				<button type="button" onclick="calculate('2')" style="padding:25px; font-size:14px;" >2</button>				<button type="button" onclick="calculate('3')" style="padding:25px; font-size:14px;">3</button>				<button type="button" onclick="calculate('+')" style="padding:25px; font-size:14px;">+</button>				<br /><br />				<button type="button" onclick="calculate('4')"  style="padding:25px; margin-left:10px; font-size:14px;">4</button>				<button type="button" onclick="calculate('5')"  style="padding:25px; font-size:14px;">5</button>				<button type="button" onclick="calculate('6')"  style="padding:25px; font-size:14px;">6</button>				<button type="button" onclick="calculate('-')"  style="padding-top:25px; padding-bottom:25px; padding-right:27px; padding-left:27px; font-size:14px;">-</button>				<br /><br />				<button type="button" onclick="calculate('7')"  style="padding:25px; margin-left:10px; font-size:14px;">7</button>				<button type="button" onclick="calculate('8')"  style="padding:25px; font-size:14px;">8</button>				<button type="button" onclick="calculate('9')"  style="padding:25px; font-size:14px;">9</button>				<button type="button" onclick="calculate('*')"  style="padding-top:25px; padding-bottom:25px; padding-right:26px; padding-left:26px; font-size:13px;">x</button>				<br /><br />				<button type="button" onclick="cancel();"  style="padding:25px; margin-left:10px; font-size:14px;">C</button>				<button type="button" onclick="calculate('0')"  style="padding:25px; font-size:14px;">0</button>				<button type="button" onclick="solve()"  style="padding:25px; font-size:14px;">=</button>				<button type="button" onclick="calculate('/')"  style="padding-top:25px; padding-bottom:25px; padding-right:26px; padding-left:26px; font-size:14px;">/</button>			</form>	</div><script src="js/script.js"></script>	</body></html>`

## Creating the Script

This code contains the script of the application. This code will calculate your equation when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory

`function calculate(val){	document.getElementById("result").value+=val;} function cancel(){	document.getElementById("result").value='';} function solve(){	var number = document.getElementById("result").value;	var total = eval(number);	document.getElementById("result").value = total;}`

There you have it we successfully created a Simple Basic Calculator using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

### Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.