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='container'>
  <div class='calculator'>
    <div id='title'>
      <h5>CALCULATOR</h5>
    </div>
  <div id='textbox'>
    <p id='entry'>0</p>
    <p id='history'>0</p>
  </div>
  
  <div id='buttons'>
    <button class='red' value='AC'>AC</button> 
    <button class='red' value='CE'>CE</button>
    <button value='/'>/</button>
    <button value='x'>x</button>
    
    <button value='7'>7</button>
    <button value='8'>8</button>
    <button value='9'>9</button>
    <button value='-'>-</button>
    
    <button value='4'>4</button>
    <button value='5'>5</button>
    <button value='6'>6</button>
    <button value='+'>+</button>
    
    <button value='1'>1</button>
    <button value='2'>2</button>
    <button value='3'>3</button>
    
    <button id='zeroButton' value='0'>0</button>
    <button value='.'>.</button>
    <button id='equalButton' value='='>=</button>
    </div>
  </div>
</div>

<footer id="footer" class="text-center">
  <div class="container">
    Designed & Coded by Tomasz Leszczynski
  </div>
</footer>
              
            
!

CSS

              
                body {
  background-color: #A9A9A9;
  font-family: 'Orbitron', sans-serif;
  font-weight: bold;
}

.calculator {
  height: 480px;
  width: 300px;
  margin-right: auto; /* puts calculator in the middle of screen*/
  margin-left: auto; /* puts calculator in the middle of screen*/
  border: 3px solid black;
  border-radius: 15px;
  background-color: #DCDCDC; 
  margin-top: 20px;
}

#title {
  text-align: center;
  padding-top: 10px;
}

#textbox {
  text-align: right;
  width: 260px;
  border: 2px solid black;
  margin-left: auto; /* puts textbox in the middle of screen*/
  margin-right: auto;
  border-radius: 10px;
  height: 80px;
  margin-bottom: 20px; 
  background-color: #FFFAF0;
}

#entry {
  font-size: 30px;
  margin-right: 15px;
  padding: 0px;
  margin-bottom: 0px;
}

#history {
  font-size: 15px;
  margin-right: 15px;
  margin-top: 0px;
  color: #A9A9A9;
}

#buttons{
  width: 280px;
  margin-left: 3px
}

.red {
  background-color: red;
}

button {
  margin-left: 15px;
  margin-bottom: 15px;
  height: 50px;
  width: 50px;
  border: 2px solid black;
  border-radius: 10px;
  font-family: 'Orbitron', sans-serif;
  font-weight: bold;
  color: white;
  background: #696969;
}

#zeroButton {
  width: 120px;
}

#equalButton {
  position: absolute;
  height: 115px;
  margin-top: -65px;
  margin-left: 20px;
}

button:active {
  transform: translate(0px, 4px); /*moves buttons littlebit down when pushed*/
  border: 0px;
  outline: none;
}

button:focus {
  outline: none; /*removes blue border on buttons when focused*/
}

.red {
  background-color: red;
}

footer {
  margin: 20px;
}
              
            
!

JS

              
                
$(document).ready(function(){
  var valOfButton = '';
  var currTextValue = ''; //current text on screen
  var currTextHistory = '' //current text in history screen
  var limitReached = false;
  var result = 0;
  var operator = '';
  var previousOperator = false; //prevents inserting an operator twice
  var preButton = false;
  
  $('button').click(function(){
   valOfButton = $(this).attr('value');
   if ((valOfButton <= 9 && valOfButton >= 0) || valOfButton == '.')  { 
     if(valOfButton === '.' && previousOperator == true){
       $('#entry').empty();
       currTextHistory = currTextHistory;
       previousOperator = true;
     } else {
     previousOperator = false;
     
    $('#entry').empty();
      if (currTextValue == '0'){
        currTextValue = valOfButton;
        currTextHistory = currTextHistory + valOfButton;
      } else {
      currTextValue = currTextValue + valOfButton;
      currTextHistory = currTextHistory + valOfButton;
      }
     }
  if (valOfButton == '.'){
       previousOperator = true;
     }
  //check if digit limit is reached and change value to value of button
       if (limitReached){
       currTextHistory = valOfButton; 
       currTextValue = valOfButton;
       limitReached = false;
     }
      if(currTextValue.length > 9){        
        currTextHistory = 'Digit limit met';
        currTextValue = '0';
        limitReached = true;
        
      }
    if(preButton){
      currTextHistory = currTextValue;
    } 
    preButton = false;
    $('#entry').text(currTextValue);
    $('#history').text(currTextHistory);
  } else {
    
    switch (valOfButton) {
      case '+':
        updateRes(parseFloat(currTextValue), '+'); 
        previousOperator = true;
        break;
      case 'CE':
        result = 0;
        currTextValue = currTextHistory = '';
        $('#entry').text('0');
        $('#history').text('0');
        break;
        case 'AC':
        result = 0;
        currTextValue = currTextHistory = '';
        $('#entry').text('0');
        $('#history').text('0');
        break;
      case '-':
        updateRes(parseFloat(currTextValue), '-'); 
        previousOperator = true;
        break;
      case 'x':
        updateRes(parseFloat(currTextValue), 'x'); 
        previousOperator = true;
        break;
      case '/':
        updateRes(parseFloat(currTextValue), '/');
        previousOperator = true;
        break;         
      case '=':
        updateRes(parseFloat(currTextValue), '=');
        preButton = true;
        break;                      }
    
  }
  }); 
  
function updateRes(val, opr){
  preButton = false;
  currTextValue = '0';
  if(!previousOperator){
  currTextHistory += opr;
  }
  $('#history').text(currTextHistory);
  switch (operator) {    
      case "+":
				result += val;
				break;
			case "-":
				result -= val;
				break;
			case "/":
				result /= val;
				break;
			case "x":
				result *= val;
				break;
			default:
				result = val == 0 ? result : val;
                  }
  operator = opr == "=" ? "" : opr;
  if (result.toString().length > 9) {
    result = 0;
    $('#history').text('Digit limit reached');
  }
  if(opr == '='){
    currTextHistory = result;
    $('#history').text(currTextHistory);
  }
  $('#entry').text(result);
}  

});

              
            
!
999px

Console