cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <div class='container bg-silver measure-wide-ns center pt2 pb3 mt4 br4 mw5 shadow-5 relative'>
	<div id='calculator' class='measure-wide-ns  pt2 pb4 br4 mw6 h7 relative'>
		<div id='screen' class='bg-mid-gray black  h3 center  tr mt4 mh0 shadow-3 w-80 mw5 br3 o-70'>
			<div class='f3 lh-solid '>
				<p id='output' class ='overflow-hidden'>0</p>
			</div>
			<div class='f5 pr1'>
				<p id='equation' class="overflow-hidden">0</p>
			</div>
		</div>
		<div id='buttons' class='tc w-80-m mw5 w-60-l ph2 center'>
			<button class='mh1 mv2 w-20 mw3 h2 br2 shadow-3 bg-red white' onclick="allClear()">AC</button>
			<button class='mh1 mv2 w-20 mw3 h2 br2 shadow-3 bg-red white' onclick="clearInput()">CE</button>
			<button class='mh1 mv2 w-20 mw3 h2 br2 shadow-3 bg-dark-gray white' onclick="divide()">&divide;</button>
			<button class='mh1 mv2 w-20 mw3 h2 br2 shadow-3 bg-dark-gray white' onclick="multiply()">X</button>

			<button class='mh1 mv2 w-20 mw3 h2 br2 shadow-3 bg-dark-gray white' onclick="processInput(7)">7</button>
			<button class='mh1 mv2 w-20 mw3 h2 br2 shadow-3 bg-dark-gray white' onclick="processInput(8)">8</button>
			<button class='mh1 mv2 w-20 mw3 h2 br2 shadow-3 bg-dark-gray white' onclick="processInput(9)">9</button>
			<button class='mh1 mv2 w-20 mw3 h2 br2 shadow-3 bg-dark-gray white' onclick="subtract()">-</button>

			<button class='mh1 mv2 w-20 mw3 h2 br2 shadow-3 bg-dark-gray white' onclick="processInput(4)">4</button>
			<button class='mh1 mv2 w-20 mw3 mw3 h2 br2 shadow-3 bg-dark-gray white' onclick="processInput(5)">5</button>
			<button class='mh1 mv2 w-20 mw3 h2 br2 shadow-3 bg-dark-gray white' onclick="processInput(6)">6</button>
			<button class='mh1 mv2 w-20 mw3 h2 br2 shadow-3 bg-dark-gray white' onclick="add()">+</button>
		  
			<button class='mh1 w-20 mw3 h2 br2 shadow-3 bg-dark-gray white' onclick="processInput(1)">1</button>
			<button class='mh1 w-20 mw3 h2 br2 shadow-3 bg-dark-gray white' onclick="processInput(2)">2</button>
			<button class='mh1 w-20 mw3 h2 mv2 br2 shadow-3 bg-dark-gray white' onclick="processInput(3)">3</button>
			<button class='mh1 w-20 mw3 h4 mt2 fr br2 shadow-3 bg-dark-gray white' onclick="equals()">=</button>
					
			<button class='mr1 w-40 mw4 h2 br2 shadow-3 bg-dark-gray white' onclick="processInput(0)">0</button>
			<button class='mh1 w-20 mw3 mt2 h2 br2 shadow-3 bg-dark-gray white' onclick="processInput('.')">.</button>

		</div>
	</div>
</div>
            
          
!
            
              body{
	font-family: 'Orbitron', sans-serif;
}

.h4{
	max-height:5em;
}

.mw3{
	max-width: 3rem;
}

	.mw5{
		min-width: 16rem;	
	}

.container{
	max-width: 20rem;
}
Button{
	user-select: none;
} 
	
.mw4{
	width: 6.7rem;
}

            
          
!
            
              let answer = 0;
let reset = false;
let inputBox = document.getElementById('output');
let equation = document.getElementById('equation');
let fullEquation = [];

function processInput(input) {
	if (reset) {
		reset = false;
		answer = 0;
		equation.textContent = '';
		inputBox.textContent = '';
		fullEquation = [];
	}
	if (!inputBox.innerHTML.match(new RegExp(/[0-9|.]/, 'g')) || inputBox.innerHTML == 0) {
		inputBox.textContent = '';
	}

	if (equation.innerHTML == 0) {
		equation.textContent = '';
	}

	if (inputBox.innerHTML.includes('.') && input == '.') {
		inputBox.textContent = 'Invalid Input';
	} else {
		inputBox.append(input);
		equation.append(input);
	}
}

function allClear() {
	inputBox.textContent = 0;
	equation.textContent = 0;
	answer = 0;
	return;
}

function clearInput() {
	inputBox.textContent = 0;
	return;
}

function add() {
	checkLast();
	recordInput();
	addtoEverything("+");
	return;
}

function subtract() {
	checkLast();
	recordInput();
	addtoEverything("-");
	return;
}

function multiply() {
	checkLast();
	recordInput();
	addtoEverything("*");
	return;
}

function divide() {
	checkLast();
	recordInput();
	addtoEverything("/");
	return;
}

function equals() {
	if (!reset) {
		recordInput();
		answer = eval(fullEquation.join(''));
		console.log(fullEquation.join('')+'='+answer);
		equation.append("=");
		fullEquation = [];
		if(answer<100000000000){
			inputBox.textContent = Math.round(answer*10)/10;
			equation.append(Math.round(answer*10)/10);
		}
		else{
			inputBox.textContent = answer.toExponential(3);
			equation.append(answer.toExponential(3));
			
		}
		//addtoEverything(answer);
		//fullEquation = [answer];
		reset = true;
	}
	return;
}

function checkLast() {
	if (reset) {
		reset = false;
		if(answer<100000000000){
			equation.textContent = Math.round(answer*10)/10;
		}
		else{
			equation.textContent = answer.toExponential(3);	
		}
	}
	if (equation.innerHTML.length > 0) {
		let lastChar = equation.innerHTML[equation.innerHTML.length - 1].toString();
		//let lastChar = fullEquation[fullEquation.length-1].toString();
		console.log(lastChar);
		if (lastChar.match(new RegExp(/[+|\-|*|÷]/, 'g'))) {
			fullEquation.pop();
			equation.textContent = equation.innerHTML.slice(0, -1);
		}
	}
	return;
}

function recordInput() {
	if (inputBox.innerHTML[inputBox.innerHTML.length - 1].match(new RegExp(/[0-9|\.]/, 'g'))) {
		fullEquation.push(inputBox.innerHTML);
	}
	return;
}

function addtoEverything(operator) {
	fullEquation.push(operator);
	if (operator == "/") {
		operator = unescape('%F7');
	}
	equation.append(operator);
	inputBox.textContent = operator;
	return;
}
            
          
!
999px
Loading ..................

Console