Skip to content

Instantly share code, notes, and snippets.

@arjshiv
Last active January 11, 2016 02:20
Show Gist options
  • Save arjshiv/3d0b07f2735ada5891e8 to your computer and use it in GitHub Desktop.
Save arjshiv/3d0b07f2735ada5891e8 to your computer and use it in GitHub Desktop.
Calculator - Daily UI #004
<div class="calculator">
<div class="result-row row">
<div class="col-md-9 col-sm-9 col-xs-9 result">
0
</div>
</div>
<div class="input-row row">
<div class="col-md-9 col-sm-9 col-xs-9 ">
<input class="calc-input form-control" type="text" value=0></input>
</div>
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">C</div>
</div>
</div>
<div class="row">
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">7</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">8</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">9</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">+</div>
</div>
</div>
<div class="row">
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">4</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">5</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">6</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">-</div>
</div>
</div>
<div class="row">
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">1</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">2</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">3</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">÷</div>
</div>
</div>
<div class="row">
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">0</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">.</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">=</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="btn btn-default calc-button">x</div>
</div>
</div>
</div>
function makeCalculator() {
// Get all the keys from document
var keys = document.querySelectorAll('.calc-button');
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('.calc-input');
var result = document.querySelector('.result');
var inputVal = input.value;
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.value = '';
result.innerHTML = '0';
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)
result.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.value += btnVal;
// Allow minus if the string is empty
else if (inputVal == '' && btnVal == '-')
input.value += 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.value = 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.value += btnVal;
decimalAdded = true;
}
}
// if any other key is pressed, just append it
else {
input.value += btnVal;
}
// prevent page jumps
e.preventDefault();
}
}
}
window.onload = function() {
makeCalculator();
}
body {
background-color: white;
color: white;
font-family: Roboto, sans-serif;
padding: 5px;
font-size: 8px;
}
.calculator {
border-radius: 1em;
width: 25%;
margin: 0 auto;
padding: 2em;
background-color: #333;
.row {
padding: 0;
&.result-row {
font-size: 4em;
height: 1.5em;
text-align: right;
color: #2abb9b;
}
&.input-row {
padding-bottom: 1em;
vertical-align: bottom;
.calc-button, .calc-input {
height: 2em;
}
.calc-button {
padding-top: 0.75em;
}
}
.col-md-3 {
padding: 0;
}
}
.calc-input, .calc-button {
transition: 0.15s linear all;
font-size: 3em;
color: white;
background-color: transparent;
border: none;
border-radius: 0;
&:hover {
cursor: pointer;
box-shadow: none;
}
&:focus, &:active {
box-shadow: none;
}
}
.calc-input {
text-align: right;
font-family: monospace;
border-radius: 0;
border-bottom: 1px solid white;
}
.calc-button {
width: 100%;
&:hover {
color: #049372;
}
&:focus, &:active {
background-color: transparent;
color: #2abb9b;
}
}
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment