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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                <div class='calculator'>
	<div class='screen'>
		<div id='display' class='display'>0</div>
	</div>
	<button value='ac' class='btn btn-small'>AC</button>
	<button value='ce' class='btn btn-small'>CE</button>
	<button value='/'  class='btn'>&divide;</button>
	<button value='*'  class='btn btn-low btn-last'>x</button>
	
	<button value='7'  class='btn btn-light'>7</button>
	<button value='8'  class='btn btn-light'>8</button>
	<button value='9'  class='btn btn-light'>9</button>
	<button value='-'  class='btn btn-low btn-last'>–</button>
	
	<button value='4'  class='btn btn-light'>4</button>
	<button value='5'  class='btn btn-light'>5</button>
	<button value='6'  class='btn btn-light'>6</button>
	<button value='+'  class='btn btn-low btn-last'>+</button>
	
	<button value='1'  class='btn btn-light'>1</button>
	<button value='2'  class='btn btn-light'>2</button>
	<button value='3'  class='btn btn-light'>3</button>
	
	<button value='0'  class='btn btn-light btn-lastline btn-wide'>0</button>
	<button value='.'  class='btn btn-light btn-lastline'>,</button>
	<button value='='  class='btn btn-low btn-last btn-lastline btn-tall'>=</button>
</div>
<footer class='footer'>
	Design and code by <a href='https://marioortega.net' target='_blank'>Mario Ortega</a>
</footer>
              
            
!

CSS

              
                html, body {
  height: 100%;
}

body {
	font-family: 'Quicksand', sans-serif;
	font-weight: 300;
	color: #000;
	background: #1e0e2b;
	width: 302px;
	margin: 0 auto;
	margin-top: 100px;
}

a {
	color: #62576b;
	text-decoration: none;
}
a:hover {
	color: #837b8b;
}

/* Structure */

.calculator:after {
  content: "";
  display: table;
  clear: both;
}

.screen {
	background: #000;
	height: 90px;
}

.display {
	font-size: 50px;
	text-align: right;
	color: #bfbfbf;
	padding-top: 17px;
	margin: 0 20px;
}

.footer {
	font-size: 13px;
	color: #62576b;
	text-align: center;
	position: relative;
	top: -25px;
}

/* Buttons */

.btn {
	font-family: 'Quicksand', sans-serif;
	font-size: 37px;
	font-weight: 300;
	color: #978f9d;
	background: #291a36;
	float: left;
	width: 74px;
	height: 65px;
	border: none;
	border-radius: 0;
	margin: 0 2px 2px 0;
	cursor: pointer;
}
.btn:focus {
	outline: none;
}
.btn:active {
	color: #bfbfbf;
}

.btn-last {
	margin-right: 0;
}

.btn-lastline {
	margin-bottom: 0;
}

.btn-light {
	background: #2e1f3a; 
}

.btn-small {
	font-size: 22px;
	font-weight: 400;
}

.btn-tall {
	position: relative;
	top: -67px;
	height: 132px;	
}

.btn-wide {
	width: 150px;
}
              
            
!

JS

              
                $(document).ready(function() {
	
	var entry    = '';
	var value1   = '';
	var value2   = '';
	var operator = '';
	var digits   = 10;
	
	function print(str) {
		// Change point for comma in display
		if (/\./.test(str)) {
			$('#display').html(str.replace('.', ','));
		} else {
			$('#display').html(str);
		}
	}
	
	function roundDecimals(value) {
		if (value.length <= digits) {
			return value;
		}
		var c = value.indexOf('.');
		if (c === -1 || c >= digits) {
			return value;
		}
		var decimals = value.length - 1 - c; // Total number of decimals in value
		var del = value.length - digits;     // Number of decimals to delete
		var finalDecimals = decimals - del;  // Final number of decimals in value
		var power = Math.pow(10, finalDecimals);
		return (Math.round(+value * power) / power).toString();
	}
	
	function filterEntry(value, num) {
		// No more than digits limit
		if (value.length >= digits) {
			print(value);
			return value;
		}
		// Starting with 0
		if (num === '0' && value === '') {
			print('0');
			return '';
		}
		// Starting with point
		if (num === '.' && value === '') {
			print('0,');
			return '0.';
		}
		// Only one point
		if (num === '.' && /\./.test(value)) {
			print(value);
			return value;
		}
		// All is ok
		value += num;
		print(value);
		return value;
	}
  
	
  // Get entry
  $('.btn').click(function() {
    
    entry = $(this).attr('value');
    
    // Enter equal
    if (entry === '=') {
      if (operator !== '') {
        value1 = eval(value1 + operator + value2).toString();
				value1 = roundDecimals(value1);
				// No more than digits limit
				if (value1.length > digits) {
					value1 = '';
					$('#display').html('Digit limit');
				} else {
					print(value1);
				}
				operator = '';
				value2   = '';
      }
      
    // Enter AC
    } else if (entry === 'ac') {
      value1   = '';
      value2   = '';
      operator = '';
      print('0');
    
    // Enter CE
    } else if (entry === 'ce') {
      if (value1 !== '' && operator === '') {
        value1 = '';
        print('0');
      } else if (operator !== '' && value2 === '') {
        operator = '';
      } else if (value2 !== '') {
        value2 = '';
        print('0');
      }
  
    // Enter operator
    } else if (entry === '/' || entry === '*' || entry === '-' || entry === '+') {
      if (value1 === '') {
        operator = '';
      } else if (operator === '') {
        operator = entry;
      } else if (operator !== '' && value2 === '') {
        operator = entry;
      } else {
        value1 = eval(value1 + operator + value2).toString();
				value1 = roundDecimals(value1);
				// No more than digits limit
				if (value1.length > digits) {
					value1 = '';
					operator = '';
					$('#display').html('Digit limit');
				} else {
					print(value1);
					operator = entry;
				}        
        value2 = '';
      }
    
    // Enter number
    } else {
      if (operator === '') {
        value1 = filterEntry(value1, entry);
      } else {
        value2 = filterEntry(value2, entry);
      }
			
    }
		console.log('Value1 = ' + value1);
		console.log('Value2 = ' + value2);
		console.log('Operator = ' + operator);
		
  });
});
              
            
!
999px

Console