Created
July 11, 2016 17:55
-
-
Save TWilliamsUK/130dbbbf6b27e17239899b5488f907c1 to your computer and use it in GitHub Desktop.
Building a calculator with javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="apple-mobile-web-app-capable" content="yes"> | |
<title>Calculator Test</title> | |
<link rel="stylesheet" href="style.css"> | |
<link href='https://fonts.googleapis.com/css?family=Open+Sans:600,400,300' rel='stylesheet' type='text/css'> | |
</head> | |
<body> | |
<div id="wrapper"> | |
<div id="calculator"> | |
<!-- Screen and clear key --> | |
<div class="top"> | |
<span class="clear">C</span> | |
<div class="screen"></div> | |
</div> | |
<div class="keys"> | |
<!-- operators and other keys --> | |
<span>7</span> | |
<span>8</span> | |
<span>9</span> | |
<span class="operator">+</span> | |
<span>4</span> | |
<span>5</span> | |
<span>6</span> | |
<span class="operator">-</span> | |
<span>1</span> | |
<span>2</span> | |
<span>3</span> | |
<span class="operator">÷</span> | |
<span>0</span> | |
<span>.</span> | |
<span class="eval">=</span> | |
<span class="operator">x</span> | |
</div> | |
</div> | |
<div id="about"> | |
<h1>A calculator web app for your iPhone</h1> | |
<p>It features <strong>less options</strong> than your current calculator and <strong>doesn't work so well</strong> either.</p> | |
<p>Simply add this page to your Home Screen. </p> | |
</div> | |
</div> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> | |
<script src="script.js"></script> | |
</body> | |
</html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get all the keys from document | |
var keys = document.querySelectorAll('#calculator span'); | |
var operators = ['+', '-', 'x', '÷']; | |
var decimalAdded = false; | |
// Add onclick event to all the keys and perform operations | |
for(var i = 0; i < keys.length; i++) { | |
keys[i].onclick = function(e) { | |
// Get the input and button values | |
var input = document.querySelector('.screen'); | |
var inputVal = input.innerHTML; | |
var btnVal = this.innerHTML; | |
// Now, just append the key values (btnValue) to the input string and finally use javascript's eval function to get the result | |
// If clear key is pressed, erase everything | |
if(btnVal == 'C') { | |
input.innerHTML = ''; | |
decimalAdded = false; | |
} | |
// If eval key is pressed, calculate and display the result | |
else if(btnVal == '=') { | |
var equation = inputVal; | |
var lastChar = equation[equation.length - 1]; | |
// Replace all instances of x and ÷ with * and / respectively. This can be done easily using regex and the 'g' tag which will replace all instances of the matched character/substring | |
equation = equation.replace(/x/g, '*').replace(/÷/g, '/'); | |
// Final thing left to do is checking the last character of the equation. If it's an operator or a decimal, remove it | |
if(operators.indexOf(lastChar) > -1 || lastChar == '.') | |
equation = equation.replace(/.$/, ''); | |
if(equation) | |
input.innerHTML = eval(equation); | |
decimalAdded = false; | |
} | |
// Basic functionality of the calculator is complete. But there are some problems like | |
// 1. No two operators should be added consecutively. | |
// 2. The equation shouldn't start from an operator except minus | |
// 3. not more than 1 decimal should be there in a number | |
// We'll fix these issues using some simple checks | |
// indexOf works only in IE9+ | |
else if(operators.indexOf(btnVal) > -1) { | |
// Operator is clicked | |
// Get the last character from the equation | |
var lastChar = inputVal[inputVal.length - 1]; | |
// Only add operator if input is not empty and there is no operator at the last | |
if(inputVal != '' && operators.indexOf(lastChar) == -1) | |
input.innerHTML += btnVal; | |
// Allow minus if the string is empty | |
else if(inputVal == '' && btnVal == '-') | |
input.innerHTML += btnVal; | |
// Replace the last operator (if exists) with the newly pressed operator | |
if(operators.indexOf(lastChar) > -1 && inputVal.length > 1) { | |
// Here, '.' matches any character while $ denotes the end of string, so anything (will be an operator in this case) at the end of string will get replaced by new operator | |
input.innerHTML = inputVal.replace(/.$/, btnVal); | |
} | |
decimalAdded =false; | |
} | |
// Now only the decimal problem is left. We can solve it easily using a flag 'decimalAdded' which we'll set once the decimal is added and prevent more decimals to be added once it's set. It will be reset when an operator, eval or clear key is pressed. | |
else if(btnVal == '.') { | |
if(!decimalAdded) { | |
input.innerHTML += btnVal; | |
decimalAdded = true; | |
} | |
} | |
// if any other key is pressed, just append it | |
else { | |
input.innerHTML += btnVal; | |
} | |
// prevent page jumps | |
e.preventDefault(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Basic reset */ | |
* { | |
margin: 0; | |
padding: 0; | |
box-sizing: border-box; | |
/* Better text styling */ | |
font-family: 'Open Sans', sans-serif; | |
font-size: 25px; | |
font-weight: 300; | |
} | |
body { | |
background: #565656; | |
} | |
#about h1 { | |
color: #FFF0F5; | |
font-size: 1.6rem; | |
font-weight: 600; | |
padding-bottom: 25px; | |
} | |
#about p { | |
color: #FFF0F5; | |
font-size: .8rem; | |
padding-bottom: 10px; | |
} | |
strong { | |
font-size: .8rem; | |
font-weight: 600; | |
} | |
#wrapper { | |
width: 750px; | |
margin: auto; | |
} | |
#about { | |
width: 300px; | |
margin: 100px 30px; | |
padding-top: 50px; | |
display: block; | |
float: left; | |
} | |
#calculator { | |
width: 305px; | |
height: auto; | |
margin: 100px 30px; | |
padding: 35px 20px 45px; | |
background: #ddd865; | |
border-radius: 15px; | |
display: block; | |
float: left; | |
} | |
/* Numbers Field */ | |
.top span.clear { | |
float: left; | |
} | |
/* Buttons */ | |
.top .screen { | |
height: 66px; | |
width: 180px; | |
overflow: hidden; | |
float: right; | |
padding: 0 10px; | |
/* Typography */ | |
font-size: 30px; | |
line-height: 58px; | |
color: #FFF0F5; | |
text-align: right; | |
letter-spacing: 2px; | |
} | |
/* Clear floats */ | |
.keys, .top {overflow: hidden;} | |
/* Applying same to the keys */ | |
.keys span, .top span.clear { | |
float: left; | |
position: relative; | |
top: 0; | |
cursor: pointer; | |
width: 61px; | |
height: 61px; | |
border-radius: 50%; | |
margin: 0 7px 11px 0; | |
padding-top: 11px; | |
color: #c1c1c1; | |
line-height: 36px; | |
text-align: center; | |
/* prevent selection of text inside keys */ | |
user-select: none; | |
/* Smoothing out hover and active states using css3 transitions */ | |
transition: all 0.3s ease; | |
} | |
/* Remove right margins from operator keys */ | |
/* style different type of keys (operators/evaluate/clear) differently */ | |
.keys span.operator { | |
background: #FFF0F5; | |
margin-right: 0; | |
} | |
.keys span.eval { | |
background: #f1ff92; | |
color: #888e5f; | |
} | |
.top span.clear { | |
background: #ff9fa8; | |
color: white; | |
} | |
/* Some hover effects */ | |
.keys span:hover, | |
.keys span.operator:hover, | |
.keys span.eval:hover, | |
.top span.clear:hover { | |
box-shadow: none; | |
color: white; | |
background: #234567; | |
} | |
.keys span, | |
.keys span.operator, | |
.keys span.eval, | |
.top span.clear { | |
box-shadow: inset 0px 3px 3px rgba(255,255,255,0.1), | |
0px 3px 3px rgba(0,0,0,0.1), | |
inset 0px -3px 3px rgba(0,0,0,0.1); | |
} | |
#calculator { | |
box-shadow: 0px 10px 15px rgba(0, 0, 0, 0.2), | |
inset 0px 3px 3px rgba(255,255,255,0.1), | |
0px 3px 3px rgba(0,0,0,0.1), | |
inset 0px -3px 3px rgba(0,0,0,0.1); | |
} | |
@media screen and (max-width: 480px) { | |
#about { | |
display: none; | |
} | |
} | |
/* A nice BG gradient | |
html { | |
height: 100%; | |
background: radial-gradient(circle, #fff 45%, #888); | |
background-size: cover; | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test
This is a test Gist to see how we go.