Calculator

Developer
Size
3,043 Kb
Views
8,096

How do I make an calculator?

What is a calculator? How do you make a calculator? This script and codes were developed by Adz on 04 November 2022, Friday.

Calculator Previews

Calculator - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Calculator</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <body><h1><center>Basic On-Screen Calculator</center></h1>
<div id="calcBody"> <div id="screen"></div>
<div id ="keyPad"> <span>9</span> <span>8</span> <span>7</span> <span id="operators">/</span> <p> <span>6</span> <span>5</span> <span>4</span> <span id="operators">*</span> <p> <span>3</span> <span>2</span> <span>1</span> <span>-</span> <p> <span id="clear">c</span> <span>0</span> <span id="eval">=</span> <span id="operators">+</span> </div> </div> </body> <script src='https://code.jquery.com/jquery-2.2.4.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Calculator - Script Codes CSS Codes

@import 'https://fonts.googleapis.com/css?family=Baloo+Bhaina';
body{font-family: 'Baloo Bhaina', cursive; background-image: url('https://upload.wikimedia.org/wikipedia/commons/c/cb/Balsa_Wood_Texture.jpg'); background-size:cover; color:#383838}
h1{text-shadow: 1px 1px white;}
#calcBody{padding:20px 0px 10px 20px; border: 2px solid #383838; width:160px; border-radius:10px; background-color:#5DCFF5; margin:auto; box-shadow: 1px 2px 7px black;}
#screen
{border:2px solid #383838; margin:0px 0px 20px 0px; width:130px; height:20px; border-radius:4px; background-color:#D6D6D6; color:#383838; overflow:scroll;padding:3px; box-shadow: 1px 0px 6px black;}
#keyPad{width:140px;text-align:center;}
span{display:inline; padding:0px 10px; border:2px solid #383838; border-radius:10px; background-color:pink; color:#383838; box-shadow: 1px 1px black;}

Calculator - Script Codes JS Codes

var keys = document.querySelectorAll("#calcBody span"); // assigns all spans to this variable
var operators = ['+', '-', '*', '/'];
for(var i = 0; i < keys.length; i++) {	keys[i].onclick = function(e) { //for all the keys(span), add onclick to each to perform the following operations//	var input = document.querySelector('#screen'); // assign screen div input var	var inputVal = input.innerHTML; //assign screen's inner HTML(text) to inputVal variable.	var btnVal = this.innerHTML; //assign text of the button clicked to btnVal.	if(btnVal == 'c') { //if "C" is pressed	input.innerHTML = " "; //clear screen	} else if(btnVal == '=') { //if "=" is pressed..	var equation = inputVal; // assign everything on screen to the equation variable;	var lastChar = equation[equation.length - 1]; // lastChar is equivalent to the last character on the screen. if(operators.indexOf(lastChar) > -1){ // if the lastCharacter of equation is an operator...	equation = equation.replace(/.$/, '');// replace it with clear right before "=" button is clicked, } if(equation) {	input.innerHTML = eval(equation); //Eval executes argument in parantheses. Equation variable stores an argument on the screen. The innerHTML of the screen, assigned to input variable, will show the result of the equation being executed upon "="clicked.	} } else if (operators.indexOf(btnVal) > -1) { // if the value of the span/btnVal is an operator	var lastChar = inputVal[inputVal.length - 1]; // lastChar = whatever the last value is on the screen. if(inputVal != '' && operators.indexOf(lastChar) == -1) //if screen isn't empty and the lastchar isn't an operator	input.innerHTML += btnVal; //add whatever the btnVal is to the screen	else if(inputVal == '' && btnVal == '-') //if screen is empty and the minus sign is clicked...	input.innerHTML += btnVal; //append it to the screen.	if(operators.indexOf(lastChar) > -1 && inputVal.length > 1) { //if the last character is an operator and the screen has more than 1 character on it...	input.innerHTML = inputVal.replace(/.$/, btnVal); //REPLACE IT WITH WHATEVER BUTTON IS CLICKED NEXT, given that it's an operator bc we are working inside the if condition that it is	} }	else {	input.innerHTML += btnVal; //otherwise just append whatever is pressed to the input.	}	e.preventDefault(); // prevent page jumps?	}
}
Calculator - Script Codes
Calculator - Script Codes
Home Page Home
Developer Adz
Username aadesida
Uploaded November 04, 2022
Rating 3
Size 3,043 Kb
Views 8,096
Do you need developer help for Calculator?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Adz (aadesida) Script Codes
Create amazing Facebook ads with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!