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.

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

            
          
!
            
              /* 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 */
}

            
          
!
            
              // 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
🕑 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.

Console