Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                <div class="container">
	 <div id="display">
	 	<h2 id="screen">0</h2>
	 </div>

	 <div id="btn-container">
	 	<div class="btn" id="clearAll"><span>AC</span></div>
		<div class="btn" id="clearLast"><span>CE</span></div>
		<div class="btn inp" id="div"><span>/</span></div>
		<div class="btn inp" id="mult"><span>*</span></div>
		<div class="btn inp"><span>7</span></div>
		<div class="btn inp"><span>8</span></div>
		<div class="btn inp"><span>9</span></div>
		<div class="btn inp" id="sub"><span>-</span></div>
		<div class="btn inp"><span>4</span></div>
		<div class="btn inp"><span>5</span></div>
		<div class="btn inp"><span>6</span></div>
		<div class="btn inp" id="add"><span>+</span></div>
		<div class="btn inp"><span>1</span></div>
		<div class="btn inp"><span>2</span></div>
		<div class="btn inp"><span>3</span></div>
		<div class="btn" id="equal"><span>=</span></div>
		<div class="btn inp zer"><span>0</span></div>
		<div class="btn inp" id="dot"><span>.</span></div>		
	 </div><!-- end btn-container -->
</div><!-- end container -->
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Roboto+Mono');

*, *:before, *:after {
	box-sizing: border-box;
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	padding: 0;
	margin: 0;
}

body {
	background: #020000;
}

.container {
	background-color: #00e5ff;
	border: 1px solid #1b0000;
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	width: 550px;
	overflow: hidden;
}

#display {
	background: #000;
	border: 1px solid black;
	width: 100%;
	height: 100px;
	text-align: right;
}

#screen {
	font-size: 2em;
	font-family: 'Roboto Mono';
	color: #F4511E;
	text-shadow: 0px 0px 1px #F4511E;
	padding: 0.9em;
}

#btn-container {
	background: black;
	width: 100%;
	height: 100%;
	margin-top: 2px;
	display: grid;
	grid-template-columns: repeat(4, 1fr);
	grid-template-rows: repeat(auto, 1fr);
	grid-gap: 0.1em;
	grid-template-areas:
	    "ac ce  sqrt multi"
	    "seven eight nine minus"
	    "four five six plus"
	    "one two three equal"
	    "zer zer point equal";
	overflow: hidden;
}

.btn {
	border: 1px solid #1b0000;
	background: black;
	font-size: 1.5em;
	font-family: 'Roboto Mono';
	color: #F4511E;
	text-align: center;
}

.btn span {
	display: inline-block;
	margin: 1.3em;
	transition: all 50ms ease-in-out;
}

.btn:active span {
	color: #00e5ff;
}

#equal {
	grid-area: equal;
	padding-top: 50px;
}

.zer {
	grid-area: zer;
}

@media (max-width: 576px) {

	body, html {
		height: 100vh;
	}

	.container {
        width: 100%;
        height: auto;	
		overflow: hidden;
	}

	#display {
		height: 90px;
	}

	#screen {
		font-size: 1.5em;
		padding: 1.2em;
	}

	#btn-container {
		padding: 0;
	}

	.btn {
		font-size: 1.3em;
	}
}
              
            
!

JS

              
                let ele = document.getElementsByClassName('inp');
const operators = ['+', '-', '/', '*'];
const clearAll = document.getElementById('clearAll');
const clearLast = document.getElementById('clearLast');
const equals = document.getElementById('equal');
let screen = document.getElementById('screen');
let chkDec = /\./; //helper to check if a number has a decimal
let input = [];
let expression = [];
let action = [];
let hold = [];
let left;
let right;
let oper;
let temp = '';
let result;

const operations = {
	'+': function(a, b){return a + b},
	'-': function(a, b){return a - b},
	'/': function(a, b){return a / b},
	'*': function(a, b){return a * b}
}

//helper function to remove empty array elements
Array.prototype.clean = function() {
	for(let i = 0; i < this.length; i++) {
		if(this[i] === '') {
			this.splice(i, 1);
		}
	}
	return this;
}

//evaluate the given expression and display the result
function evaluate() {

	//clean array of empty elements
	expression.clean();
	while(expression.length > 1) {
		left = expression.shift();
		right = expression.shift();
		oper = action.shift();

		if(chkDec.test(left) || chkDec.test(right)) {
			result = operations[oper](parseFloat(left, 10), parseFloat(right, 10));
		}
		else {
			result = operations[oper](parseInt(left, 10), parseInt(right, 10));
		}

		//convert the result back to a string
		result = result.toString();

		//push result back into the expression queue
		//so we can keep doing math stuff
		expression.unshift(result);
	}


	if(result == undefined || isNaN(result)) {
		screen.innerText = 'Err';
	} else if(expression.length == 1 && action.length > 0) {
		screen.innerText = 'Err';
	} else {
		screen.innerText = result;
	}

}

//push each ele into the appropriate stack/queue
function numberFactory(arr) {
	let chk; 
	while(arr.length > 0) {
		chk = arr.shift();
		checkInput(chk);
	}

	evaluate();
}

//flatten decimal and double digit numbers in the input array
function flattenDec(arr) {
	for(let i = 0; i < arr.length; i++) {
		if(operators.includes(arr[i])) {
			hold.push(temp);
			hold.push(arr[i]);
			temp = '';
		}

		if(!operators.includes(arr[i])) {
			temp = temp += arr[i];
		}
	}
	input.length = 0;
	hold.push(temp);
	temp = '';

	numberFactory(hold);
}

//check for number or operand and push
//to the appropriate array
function checkInput(ele) {
	if(operators.includes(ele)) {
		action.push(ele);
	} else {
		expression.push(ele);
	}
}

//clear all input data and results
function emptyAll() {
	input.length = 0;
	action.length = 0;
	hold.length = 0;
	expression.length = 0;
	right = undefined;
	left = undefined;
	oper = undefined;
	result = undefined;
	screen.innerText = 0;
}

//clear last element from input array
function emptyLast() {
	input.pop();
	display(input);
}

//display output on screen 
function display(arr) {
	screen.innerText = '';
	if(arr.length <= 0) {
		screen.innerText = result || 0;
	}

	arr.forEach(x => {
		screen.innerText += x;
	});
}

//setup event listeners
//limit input array to 22 chars
for(let i = 0; i < ele.length; i++) {
	ele[i].addEventListener('click', event => {
		if(input.length < 22) {
			input.push(ele[i].firstChild.innerText);
			display(input);
		}
	});
}

clearAll.addEventListener('click', event => {
	emptyAll();
});

clearLast.addEventListener('click', event => {
	emptyLast();
});

equals.addEventListener('click', event => {
	//check user input and calls for expression
	//to be evaluated if necessary or displays Err to user
	if(expression.length == 1 && action.length == 0 && input.length == 0) {
		screen.innerText = expression[0];
	} else if(input.length == 1 && expression.length == 0 && action.length == 0) {
		if(operators.includes(input[0]) || input[0] == '.') {
			screen.innerText = 'Err';
		} else {
			screen.innerText = input[0];
		}
	} else if(input.length == 0 && action.length == 0 && expression.length == 0) {
		screen.innerText = 0;
	} else if(input.length > 1 && action.length == 0) {
		flattenDec(input);
	}

});
              
            
!
999px

Console