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 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

              
                <h1 class = 'disclaimer'>JS Calculator</h1>

<div id = 'container' class = 'noselect'>

<div id = 'calcBody'>
<div id = 'top'>
<span class='clear'>C</span>
<div id = 'resultScreen'>
  <div id = 'result'></div>
  </div>  
</div>
  
<div id = 'buttonSection'>
  <span class = 'number'>7</span>
    <span class = 'number'>8</span>
    <span class='number'>9</span>
    <span class='operator'>x</span>
    <span class='number'>4</span>  
    <span class='number'>5</span>
    <span class='number'>6</span>
   <span class='operator'>÷</span>
    <span class='number'>1</span>
    <span class='number'>2</span>  
    <span class='number'>3</span>
     <span class='operator'>+</span>
    
    <span class='number'>0</span>
    <span class='number'>.</span>
    <span class='equal'>=</span >
     <span class='operator'>-</span>
    
   
  
  </div>
</div>
</div>

<p class='disclaimer'>Coded by <a href='http://www.maxvl.com'>Max Van Lyl</p>

              
            
!

CSS

              
                /* Future improvements ideas 
- adding hover states on buttons
- making font a bit larger and more 'calculatory'
- making a more photorealistic calculator
*/

* {
	margin: 0;
	padding: 0;
	box-sizing: border-box;
	font: bold 14px Arial, sans-serif;

}




#calcBody {
  margin: auto;
  margin-top: 50px;
  height: 300px;
  width: 400px;
  background: #9dd2ea;
	background: linear-gradient(#9dd2ea, #8bceec);
	border-radius: 3px;
	box-shadow: 0px 4px #009de4, 0px 10px 15px rgba(0, 0, 0, 0.2);

}

#resultScreen {
  border: 1px solid black;
  width: 270px;
  height:30px;
  margin-top: 15px;
  margin-right: 20px;
  background-color: #456783;
  float: right;
	background: rgba(0, 0, 0, 0.2);
	border-radius: 3px;
	box-shadow: inset 0px 4px rgba(0, 0, 0, 0.2);
}

#result {
  height: 29px;
  width: 270px;
  overflow: hidden;
  text-align: right;
  padding-right: 10px;
  font-size: 20px;
}

#buttonSection {
  margin-top: 20px;
  margin-left: 10px
  
}


#buttonSection span {
  float: left;
	width: 70px;
	height: 40px;
	background: white;
	border-radius: 3px;
	margin: 0 10px 11px;
	color: #888;
	line-height: 40px;
	text-align: center;
}

#buttonSection span.operator {
	background-color: #456745;
	margin-right: 20px;
  float: right;
}

#buttonSection span.equal {
  background-color: green;
}

#top {
  height: 60px;
  
}

.clear {
 background-color: red;
 display: inline-block;
 width: 70px;
 height: 40px;
 border-radius: 3px;
  text-align: center;
  line-height: 40px;
  margin-top: 10px;
  margin-left: 20px;
}

.disclaimer {
  text-align: center;
  margin-top: 30px;
}

/* not allowing text to be selected on the calculator */
.noselect {
  -webkit-touch-callout: none; /* iOS Safari */
    -webkit-user-select: none; /* Chrome/Safari/Opera */
     -khtml-user-select: none; /* Konqueror */
       -moz-user-select: none; /* Firefox */
        -ms-user-select: none; /* Internet Explorer/Edge */
            user-select: none; /* Non-prefixed version, currently
                                  not supported by any browser */
}

              
            
!

JS

              
                // Future improvement ideas: 
// Figure out what evils the eval function has 
// add more safety checks

$(document).ready(function() {

  var totalString = '';
  var operators = ['+', '-', 'x', '÷'];

  //digit Entry + digit combining  
  $('.number').on('click', function() {
    var buttonEntry = $(this).text();
    totalString += buttonEntry;
    console.log("CombinedDigits while pressing a number: " + totalString);
    $('#result').html('<p>' + totalString + '</p>');
  });

  // adding operators to the total string that will be evaluated
  $('.operator').on('click', function() {
    var opVal = this.innerHTML;
    totalString += opVal;
    $('#result').html('<p>' + totalString + '</p>');
    console.log(totalString);
  });

  $('.equal').on('click', function() {

    var lastChar = totalString[totalString.length - 1];
    //replace the operator strings with actual operators
    var equation = totalString.replace(/x/g, '*').replace(/÷/g, '/');
    // check if last character is an operator, if so, remove it.
    if (operators.indexOf(lastChar) > -1) {
      equation = equation.slice(0, -1);
      console.log(equation);
    }

    var result = eval(equation);
    $('#result').html('<p>' + result + '</p>');
    console.log('final result: ' + result);
  });

  //when pressing the clear button, clean it all up
  $('.clear').on('click', function() {
    $('#result').html('<p>');
    totalString = '';
  });
});
              
            
!
999px

Console