css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

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.

            
              <div id="container">
	<div class="cover">
		<h1>FreeCodeCamp <br />Calculator</h1> 
		<!-- First is just instructions for user input -->
		<p id="first" class='first-line'>Only use these characters</p>
		<p class="minus-padding"> 0-9 , * , / , - , + , = </p>
	</div>
	<div class="calculator">
		<section class="head">
			<div id="displayField">
				<span class="blinking-cursor">|</span>
				<span id="equals" class="numbers"></span> 
			</div>
		</section>
		<div class="btnSection">
			<div class="row row-top" id="center clear">
				<div id="allClear" class="center col dub clear">Clear All</div>
				<div id="clearOne" class="center col dub clear">Clear </div>
			</div>
			<div class="row row1">
				<div id="seven" class="center col num solo" data-value="7">7</div>
				<div id="eight" class="center col num solo" data-value="8">8</div>
				<div id="nine" class="center col num solo" data-value="9">9</div>
				<div id="divide" class="center col solo symbols" data-value="/">/</div>
			</div>
			<div class="row row2">
				<div id="four" class="center col num solo"  data-value="4">4</div>
				<div id="five" class="center col num solo" data-value="5">5</div>
				<div id="six" class="center col num solo"  data-value="6">6</div>
				<div id="multiply" class="center col solo symbols" data-hamburger="*">*</div>
			</div>
			<div class="row row3">
				<div id="one" class="center col num solo" data-value="1">1</div>
				<div id="two" class="center col num solo" data-value="2">2</div>
				<div id="three" class="center col num solo" data-value="3">3</div>
				<div id="subtract" class="center col solo symbols" data-value="-">-</div>
			</div>
			<div class="row">
				<div id="zero" class="center col num dub" data-value="0">0</div>
				<div id="decimal" class="center col solo symbols" data-value=".">.</div>
				<div id="add" class="center col solo symbols" data-value="+">+</div>
			</div>
			<div class="row">
				<div id="equal" class="col dub red" data-value="=">=</div>
			</div>
		</div>
	</div>
</div>
            
          
!
            
              * { margin: 0px; padding: 0; box-sizing: border-box; }

html, body {
	background-color: whitesmoke;
	font-size: 25px;
	font-family: Helvetica, sans-serif;
	font-weight: 900;
	position: relative;
}
body {
	display: flex;
	justify-content: center;
	align-items: center;
}
#container {
	display: flex;
	flex-direction: column;
	max-width: 300px;
}

.cover {
	text-align: center;
}

.cover > h1 { 
	padding-top: 10px;
	border-top: 2px black solid;
	margin-top: 30px;
	font-size: 1.6rem;
	color: rgba(0,0,0,0.6);
	text-shadow: 3px 3px 3px rgba(255,255,255,0.8);
}

.cover > p { 
	font-size: 1rem;
}

p:nth-child(2), #second {
	color: whitesmoke;
	margin-top: 6px;
	margin-bottom: 3px;
	background-color: red;
	padding: 5px 0;
	text-shadow: 1px 1px 2px #191919;
}

.minus-padding {
	margin-bottom: -50px;
	padding: 5px 0;
	color: rgba(0,0,0,0.6);
	text-shadow: 3px 3px 3px rgba(255,255,255,0.8);
 	border-bottom: 2px black solid;
}

.calculator {
	//display: flex;
	flex-direction: column;
  	justify-content: center;
 	margin: 3em 0;
	width: 100%;
	border-radius: 10px;
}

.head, .btnSection {
	display: flex;
	width: 100%;
	flex-direction: column;
}

#second {
	display: none;
}

#displayField {
	height: 50px;
	font-size: 1rem;
	padding: 10px;
	padding-bottom: 30px;
	text-align: right;
	border: 1px black solid;
	border-radius: 10px 10px 0 0;
	box-shadow: 1px 1px 3px 1px rgba(0,0,0,0.3),
					-1px -1px 3px 1px rgba(0,0,0,0.3);
}

.row {	
	display: flex;
	height: 50px; 
}

.row > .red {
	border-radius: 0 0 10px 10px;
	height: 100px;
	background-color: red;
	color: white;
	font-size: 3em;
	text-shadow: 2px 2px 5px black;
}
.col {
	text-align: center;
	border: 1px black solid;
	//margin: 2px;
}

.dub {
	flex-grow: 3;
}
.solo {
	flex-grow: 1;
}
.symbols {
	flex-basis: 50px;
	color: whitesmoke;
	background-color: darkblue;
	text-shadow: 2px 2px 4px #191919;
}
.num {
	background-color: #209cee;
	color: whitesmoke;
	text-shadow: 1px 1px 4px #191919;
}
.clear {
	background-color: lightgreen;
	color: whitesmoke;	
	text-shadow: 1px 1px 4px black;
}
.center { padding-top: 8px; }

.blinking-cursor {
	font-weight: 200;
	line-height: 30px;
  	animation: 3s blink step-end infinite;
}

@keyframes "blink" {
  from, to {
    color: transparent;
  }
  50% {
    color: black;
  }
}

            
          
!
            
              //Input!! 
//document.querySelectorAll(cssSelect)
//document.querySelector(cssSelect)
const nine = getSelector('#nine')
const eight = getSelector('#eight')
const seven = getSelector('#seven')
const six = getSelector('#six')
const five = getSelector('#five')
const four = getSelector('#four')
const three = getSelector('#three')
const two = getSelector('#two')
const one = getSelector('#one')
const zero = getSelector('#zero')

// Operators
const subtract = getSelector('#subtract') 
const add = getSelector('#add') 
const divide = getSelector('#divide') 
const multiply = getSelector('#multiply') 
/* Fix multiply using regex - replace("X","*") */
const decimal = getSelector('#decimal') 
const equal = getSelector('#equal')

// Output!!
let equals = getSelector('#equals')

// Clears
const allClear = getSelector('#allClear') 
const clearOne = getSelector('#clearOne') 

// Test Event Listner Builder
/* */
function buildListner (id){
	id.addEventListener('click', function() {
	  equals.innerHTML = equals.innerHTML + id.innerHTML;
	})
}// Could use an array as well? Done! See below
const numbers = [zero,one,two,three,four,five,six,seven,eight,nine]
numbers.forEach(number => buildListner(number))

/* Version 2

function buildListner (id){
	id.addEventListener('click', buildListnerResult)
}
function buildListnerResult (id){
	equals.innerHTML = equals.innerHTML + id.innerHTML;
}

//version 3 - use array
operators.forEach(operator => buildListner(operator))

*/
/* // Version 1: Number Listeners
buildListner(zero)
buildListner(one)
buildListner(two)
buildListner(three)
buildListner(four)
buildListner(five)
buildListner(six)
buildListner(seven)
buildListner(eight)
buildListner(nine)
*/
/* zero.addEventListener('click', function() {
  equals.innerHTML = equals.innerHTML + zero.innerHTML;
}) 
one.addEventListener('click', function() {
  equals.innerHTML = equals.innerHTML + one.innerHTML;
})
two.addEventListener('click', function() {
  equals.innerHTML = equals.innerHTML + two.innerHTML;
})
three.addEventListener('click', function() {
  equals.innerHTML = equals.innerHTML + three.innerHTML;
})
four.addEventListener('click', function() {
  equals.innerHTML = equals.innerHTML + four.innerHTML;
})

five.addEventListener('click', function() {
  equals.innerHTML = equals.innerHTML + five.innerHTML;
})
six.addEventListener('click', function() {
  equals.innerHTML = equals.innerHTML + six.innerHTML;
})
seven.addEventListener('click', function() {
  equals.innerHTML = equals.innerHTML + seven.innerHTML;
})
eight.addEventListener('click', function() {
  equals.innerHTML = equals.innerHTML + eight.innerHTML;
})
nine.addEventListener('click', function() {
  equals.innerHTML = equals.innerHTML + nine.innerHTML;
})
*/
// Operator Listners
// Try with Array
const operators =  [divide, multiply, subtract, decimal, add]
operators.forEach(operator => buildListner(operator))

equal.addEventListener('click', function() {
  equals.innerHTML = eval(equals.innerHTML);
})

// Clear output 
allClear.addEventListener('click', clearAllData)
clearOne.addEventListener('click', clearOneData)

//Clear data when called
function clearAllData(){
	equals.innerHTML='';
}
function clearOneData (){
	let str = equals.innerHTML
	shrinkString(str)
}
function shrinkString (val) {
		equals.innerHTML = val.substr(0,val.length-1)
}

// Helper Functions for Listners/Selectors
// Use for Numbers & Operators
function getAllSelectors (cssSelect) {
	return document.querySelectorAll(cssSelect)
}
// Use for symbols and Utilities on Calculator
function getSelector(cssSelect){
	return document.querySelector(cssSelect)
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console