Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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="box">
    <div class="screen">
      <div class="main-screen" id="output">0</div>
      <div class="sub-screen" id="output2"></div>
    </div>

    <input type="hidden" id="num1">
    <input type="hidden" id="operator">
    <input type="hidden" id="num2">
    <input type="hidden" id="temp">

    <!--a class="github" href="https://github.com/greyli/calculator" target="_blank"><img style="position: absolute; top: 0; right: 0; border: 0;" src="image/banner.png" alt="Fork me on GitHub"></a-->

    <div class="container-fluid">
      <div class="buttons">
        <button class="btn-clear btn btn-danger" id="clearButton">AC</button>
        <button class="btn btn-warning" id="deleteButton">CE</button>
        <button class="btn-operate btn btn-default" value="/">/</button>
        <button class="btn-operate btn btn-default" value="*">x</button>
      </div>

      <div class="buttons">
        <button class="nums btn btn-default" value="7">7</button>
        <button class="nums btn btn-default" value="8">8</button>
        <button class="nums btn btn-default" value="9">9</button>
        <button class="btn-operate btn btn-default" value="-">-</button>
      </div>

      <div class="buttons">
        <button class="nums btn btn-default" value="4">4</button>
        <button class="nums btn btn-default" value="5">5</button>
        <button class="nums btn btn-default" value="6">6</button>
        <button class="btn-operate btn btn-default" value="+">+</button>
      </div>

      <div class="buttons">
        <button class="nums btn btn-default" value="1">1</button>
        <button class="nums btn btn-default" value="2">2</button>
        <button class="nums btn btn-default" value="3">3</button>
        <button class="btn-equal btn btn-info" id="resultButton">=</button>
      </div>

      <div class="buttons">
        <button class="nums btn-zero btn btn-default" value="0">0</button>
        <button class="nums btn btn-default" value=".">.</button>
      </div>
    </div>
  </div>
  <br>
  <footer class="text-center">
      Made by <a href="http://greyli.com" target="_blank">Grey Li</a> /
        Fork me on <a href="https://github.com/greyli/calculator" target="_blank">GitHub</a>
  </footer>
              
            
!

CSS

              
                .box {
  font-family: 'Orbitron', sans-serif;
  width: 285px;
  height: 350px;
  margin: 15% auto auto;
  border: 1px solid #9e9b97;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), inset -1px -6px 12px 0.1px #89847e;
  border-radius: 20px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.screen {
  height: 60px;
  width: 240px;
  border: 1px solid #7c877f;
  background: #c7d3c5;
  margin: 25px 20px 5px 20px;
  border-radius: 6px;
}

.main-screen {
  width: 240px;
  height: 22px;
  padding: 10px 5px;
  font-size: 20px;
  text-align: right;
}

.sub-screen {
  max-width: 280px;
  height: 15px;
  padding: 10px 5px;
  font-size: 12px;
  text-align: right;
}

.buttons {
  margin: 10px auto;
}

button {
  margin: 5px;
  padding: 3px 10px;
  width: 50px;
  height: 30px;
  border: none;
  box-shadow: 1px 2px #666;
}

button:focus {outline:0 !important;}

button:active {
  box-shadow: none;
  transform: translateY(4px);
}

.btn-zero {
  width: 113px;
  margin-right: 7px;
}

.btn-equal {
  position: absolute;
  margin-left: 10px;
  height: 75px;
}
              
            
!

JS

              
                // todo: calculate based on result

  $(document).ready(function() {
  var mainOutput = $('#output');
  var subOutput = $('#output2');
  var op = $('#operator');
  var num2 = $('#num2');
  var num1 = $('#num1');
  var temp = $('#temp');
  var clearData = function() {
    num1.val('');
    op.val('');
    num2.val('');
    temp.val('');
  };

  var clearOutput = function() {
    mainOutput.html('');
    subOutput.html('');
  };

  var digitError = function() {
    //mainOutput.html('0');
    subOutput.html('Reach Digit Limit');
    temp.val(0);
  }

  $('.nums').click(function() {
    if (('+-*/').indexOf(mainOutput.html()) != -1) {
      mainOutput.html('');
    }
    // avoid multiple dot
    if ($(this).val() == '.' && (mainOutput.html()).indexOf('.') != -1) return;

    if (mainOutput.html() == '0') {
      clearOutput()
    }

    // clear output for new input
    if (temp.val() !== '') {
      clearOutput()
      clearData();
    }

    if (mainOutput.html().length > 12) {
      subOutput.html('Reach Digit Limit');
    } else {
      mainOutput.append($(this).val());
      subOutput.append($(this).val());
    }

  });

  $('#clearButton').click(function() {
    mainOutput.html('0');
    subOutput.html('');
    clearData();
  });

  $('#deleteButton').click(function() {
    if (subOutput.html() == 'Reach Digit Limit') {
      subOutput.html('');
    }
    if (mainOutput.html() != '0') {
      mainOutput.html(mainOutput.html().substring(0, mainOutput.html().length - 1));
      subOutput.html(subOutput.html().substring(0, subOutput.html().length - 1));
      if (mainOutput.html() == '') {
        mainOutput.html('0');
      }
    }
  });

  $('.btn-operate').click(function() {
    if (subOutput.html() == 'Reach Digit Limit') {
      subOutput.html('');
    }
    var newOperator = $(this).val();
    if (num1.val() !== '' && ('+-*/').indexOf(num1.val()) == -1 && op.val() !== '') {
      num2.val(mainOutput.html());
      if (('+-*/').indexOf(num2.val()) != -1) return;
      var number1 = parseFloat(num1.val());
      var operator = op.val();
      var number2 = parseFloat(num2.val());
      var result;
      if (operator == '+') {
        result = number1 + number2;
      } else if (operator == '-') {
        result = number1 - number2;
      } else if (operator == '*') {
        result = number1 * number2;
      } else if (operator == '/') {
        result = parseFloat(number1 / number2);
      }

      if (result.toString().length > 12) {
        mainOutput.html('0');
        digitError();
      } else {
        mainOutput.html(newOperator);
        subOutput.html(result + newOperator);
        num1.val(result);
        op.val(newOperator);
      }
    } else {
      num1.val(mainOutput.html());
      op.val(newOperator);
      mainOutput.html(newOperator);
      subOutput.append(newOperator);
    }
  });

  $('#resultButton').click(function() {
    if (mainOutput.html() === '' || ('+-*/').indexOf(mainOutput.html()) != -1) return;
    num2.val(mainOutput.html());
    var number1 = parseFloat(num1.val());
    var operator = op.val();
    var number2 = parseFloat(num2.val());
    var result;
    if (operator == '+') {
      result = number1 + number2;
    } else if (operator == '-') {
      result = number1 - number2;
    } else if (operator == '*') {
      result = number1 * number2;
    } else if (operator == '/') {
      result = parseFloat(number1 / number2);
    }
    if (result.toString().length > 12) {
      mainOutput.html('0');
      digitError();
    } else {
      mainOutput.html(result);
      subOutput.html('');
      temp.val(result);
    }
  });
});
              
            
!
999px

Console