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

              
                <!-- Main page -->
<div id="main" class="container-fluid">
  <!-- 10-digit display -->
  <div id="display">
    <p id="result">0</p>
    <p id="log">0</p>
  </div>

  <!-- Buttons on the calculator -->
  <div id="allButtons">
    <!-- container for the first two columns of buttons -->
    <div id="leftButtons">
      <a href="#" class="btn clearButton"><span>C</span></a>
      <a href="#" class="btn" id="backButton"><span><</span></a>
      <a href="#" class="btn numberButton"><span>7</span></a>
      <a href="#" class="btn numberButton"><span>8</span></a>
      <a href="#" class="btn numberButton"><span>4</span></a>
      <a href="#" class="btn numberButton"><span>5</span></a>
      <a href="#" class="btn numberButton"><span>1</span></a>
      <a href="#" class="btn numberButton"><span>2</span></a>
      <a href="#" class="btn numberButton"><span>0</span></a>
      <a href="#" class="btn" id="decimalButton"><span>.</span></a>
    </div><!-- end leftButtons -->
    
    <!-- container for the third column of buttons: multiply, 9, 6, 3, and the decimal point. -->
    <div id="middleButtons">
      <a href="#" class="btn operationButton"><span>/</span></a>
      <a href="#" class="btn numberButton"><span>9</span></a>
      <a href="#" class="btn numberButton"><span>6</span></a>
      <a href="#" class="btn numberButton"><span>3</span></a>
      <a href="#" class="btn" id="absoluteButton"><span>+/-</span></a>
    </div><!-- end middleButtons -->
    
    <!-- Container for the fourth column of buttons: minus, plus, and equals. -->
    <div id="rightButtons">
      <a href="#" class="btn operationButton"><span>*</span></a>
      <a href="#" class="btn operationButton"><span>-</span></a>
      <a href="#" class="btn operationButton"><span>+</span></a>
      <a href="#" class="btn tallButton" id="equalsButton"><span>=</span></a>
    </div><!-- end rightButtons -->
  </div>
</div>

              
            
!

CSS

              
                body {
  background-color: #e2cfb6;
}

#main {
  position: relative;
  margin-top: 15%;
  height: 372px;
  width: 254px;
  background-color: #e7e3e2;
  border: 2px solid #989898;
  border-radius: 20px;
}

#display {
  float: left;
  background-color: #404040;
  height: 100px;
  width: 250px;
  margin-left: -15px;
  border-top-left-radius: 18px;
  border-top-right-radius: 18px;
}

#display p {
  background-color: #99d060;
  padding-right: 5px;
  text-align: right;
  @import url('https://fonts.googleapis.com/css?family=Orbitron');
  font-family: 'Orbitron', sans-serif;
}

#result {
  height: 40px;
  font-size: 18pt;
  margin: 20px 18px 0px;
  padding-top: 3px;
}

#log {
  height: 20px;
  font-size: 10pt;
  margin: 0px 18px 20px;
}

#allButtons {
  width: 220px;
}

#leftButtons {
  float: left;
  width: 108px;
}

#middleButtons,
#rightButtons{
  float: left;
  width: 50px;
  margin: 0px 3px;
}

/* Some individual styling for the operation buttons that appear a little smaller than the number buttons. */
#multiplyButton {
  font-size: 16pt;
}

.btn {
  display: inline-block;
  height: 40px;
  width: 45px;
  margin: 5px 3px 5px;
  /* Border-radius is set to round off the corners on each button */
  -webkit-border-radius: 8px;
  -moz-border-radius: 8px;
  border-radius: 6px;
}

.btn span {
  display: inline-block;
  height: 33px;
  width: 45px;
  margin-left: -13px;
  padding-top: 8px;
  color: white;
  font-weight: bold;
  font-size: 12pt;
  /* Border radius inside the span rounds off the edges of the spans */
  -webkit-border-radius: 8px;
  -moz-border-radius: 8px;
  border-radius: 6px;
}

.btn:active span {
  /* Transform lines make each button look like it moves up and down when it is pressed */
  -webkit-transform: translate(0, 2px);
  -moz-transform: translate(0, 2px);
  -o-transform: translate(0, 2px);
  transform: translate(0, 2px);
}

/* The coloring of the buttons is broken down into three groups: the number/decimal keys, the operation keys, and the clear key. */
.numberButton,
#decimalButton,
#absoluteButton {
  /* Box shadow adds the 3D effect of a pressable button */
  /* This section controls the size and color of the 3D effect on each button */
  -webkit-box-shadow: 0 4px 0 #8c8c8c, 0 15px 20px rgba(0, 0, 0, .25);
  -moz-box-shadow: 0 4px 0 #8c8c8c, 0 15px 20px rgba(0, 0, 0, .25);
  box-shadow: 0 4x 0 #8c8c8c, 0 15px 20px rgba(0, 0, 0, .25);
}

.numberButton:active,
#decimalButton:active,
#absoluteButton:active {
  -webkit-box-shadow: 0 4px 0 #8c8c8c, 0 12px 10px rgba(0, 0, 0, .2);
  -moz-box-shadow: 0 4px 0 #8c8c8c, 0 12px 10px rgba(0, 0, 0, .2);
  box-shadow: 0 4px 0 #8c8c8c, 0 12px 10px rgba(0, 0, 0, .2);
}

.numberButton span,
#decimalButton span,
#absoluteButton span{
  background-color: #a6a6a6;
}

.operationButton,
#backButton, 
#equalsButton {
  /* Box shadow adds the 3D effect of a pressable button */
  /* This section controls the size and color of the 3D effect on each button */
  -webkit-box-shadow: 0 4px 0 #4d4d4d, 0 15px 20px rgba(0, 0, 0, .25);
  -moz-box-shadow: 0 4px 0 #4d4d4d, 0 15px 20px rgba(0, 0, 0, .25);
  box-shadow: 0 4x 0 #4d4d4d, 0 15px 20px rgba(0, 0, 0, .25);
}

.operationButton:active,
#backButton:active,
#equalsButton:active {
  -webkit-box-shadow: 0 4px 0 #4d4d4d, 0 12px 10px rgba(0, 0, 0, .2);
  -moz-box-shadow: 0 4px 0 #4d4d4d, 0 12px 10px rgba(0, 0, 0, .2);
  box-shadow: 0 4px 0 #4d4d4d, 0 12px 10px rgba(0, 0, 0, .2);
}

.operationButton span,
#backButton span,
#equalsButton span {
  background-color: #595959;
  background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#595959), to(#6d6d6d));
  background: -moz-linear-gradient(#595959, #595959);
}

.clearButton {
  /* Box shadow adds the 3D effect of a pressable button */
  /* This section controls the size and color of the 3D effect on each button */
  -webkit-box-shadow: 0 4px 0 #b30000, 0 15px 20px rgba(0, 0, 0, .25);
  -moz-box-shadow: 0 4px 0 #b30000, 0 15px 20px rgba(0, 0, 0, .25);
  box-shadow: 0 4x 0 #b30000, 0 15px 20px rgba(0, 0, 0, .25);
}

.clearButton:active {
  -webkit-box-shadow: 0 4px 0 #b30000, 0 12px 10px rgba(0, 0, 0, .2);
  -moz-box-shadow: 0 4px 0 #b30000, 0 12px 10px rgba(0, 0, 0, .2);
  box-shadow: 0 4px 0 #b30000, 0 12px 10px rgba(0, 0, 0, .2);
}

.clearButton span {
  background-color: #ff4d4d;
}

/* Separate styles are required for the three larger buttons on the calculator */
.tallButton {
  height: 90px;
}

.tallButton span {
  height: 83px;
}

.wideButton {
  width: 100px;
}

.wideButton span {
  width: 100px;
}
              
            
!

JS

              
                // Not shown: imported JQuery library
// https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js
$(document).ready(function() {
  // When you press a number button, add that value to the display.
  // The maximum display length is 10 digits.
  $(".numberButton").on("click", function() {
    // If the result has a leading zero, clear the result.
    if ($("#result").text() === "0") {
      clearResult();
    }
    
    // If the log has a limit warning, clear the log.
    if ($("#log").text() === "Over Limit") {
      //clearResult();
      clearLog();
      addToLog("0");
    }
    
    // If an old equation exists, clear both boxes.
    if ($("#log").text().indexOf("=") >= 0) {
      clearResult();
      clearLog();
      addToLog("0");
    }

    // Make sure we don't go over the limit.
    if (countResult() < 10) {
      addToResult($(this).find("span").text());
    }
  });
  
  $("#decimalButton").on("click", function() {
    // If there is a previous equation in the log, set both log and result to 0.
    if ($("#log").text().indexOf("=") >= 0) {
      clearLog();
      addToLog("0");
      
      clearResult();
      addToResult("0");
    }
    
    // Make sure that we don't already have a decimal point in the result,
    // and that we haven't already reached the limit of the display.
    if ($("#result").text().indexOf(".") < 0 && countResult() < 10) {
      addToResult(".");
    }
  });
  
  $("#absoluteButton").on("click", function() {
    // If the result is a zero, do not add a negative sign.
    
    // If the front of the display contains a non-zero number, multiply it by -1.
    if ($("result").text().parseFloat !== 0) {
      $("#result").text(parseFloat($("#result").text() * -1));
    }
  });
  
  // If the backspace button is pressed, remove the last digit from the result and the log.
  $("#backButton").on("click", function() {
    backspace();
  });

  // When any of the four basic math operations are pressed, store the existing number on the log bar and clear the display.
  $(".operationButton").on("click", function() {
    // Only add the result to the log if it's something other than a zero.
    if ($("#result").text() !== "0") {
      // If a leading zero exists in the log, remove it.
      if ($("#log").text() == "0") {
        clearLog();
      }
      
      // If an old expression (with an equals sign in it) exists in the log, clear it.
      if ($("#log").text().indexOf("=") >= 0) {
        clearLog();
      }
      
      addToLog($("#result").text());
      addToLog($(this).find("span").text());
    }
    
    clearResult();
    addToResult("0");
  });
  
  // When you press the clear button, reset the display and the log to 0.
  $(".clearButton").on("click", function() {
    clearResult();
    addToResult("0");
    
    clearLog();
    addToLog("0");
  });
  
  $("#equalsButton").on("click", function() {
    // Only evaluate the total under the following conditions:
    // - The result is non-zero.
    // - The log is non-zero.
    // - The log does not have an equals sign already (this prevents consecutive equal buttons).
    if (parseFloat($("#result").text()) !== 0 && parseFloat($("#log").text()) !== 0 && $("#log").text().indexOf("=") < 0) {
      addToLog($("#result").text());
      
      // Known issue: subtracting a negative number.
      // Example: 2 - (-2) should be 4, but eval returns an error.
      // Solution: replace consecutive minus signs with a single plus signs in the eval text only, so the log shows the user's original result.
      $("#result").text(eval($("#log").text().replace("--","+")));
      
      addToLog("=");
      addToLog($("#result").text());
    }
  });
});

// Add the given string to the existing result.
function addToResult(newValue) {
  $("#result").text($("#result").text() + newValue);
}

// Add the given string to the existing log.
// This function validates the size of the log.
// The limit is 22 characters (two ten-digit numbers and one operation are allowed).
function addToLog(newValue) {
  $("#log").text($("#log").text() + newValue);
  
  // If the log file is over the limit, display a warning.
  if ($("#log").text().length > 22) {
    clearResult();
    addToResult("0");
    $("#log").text("Over Limit");
  }
}

// This function removes the last digit from the display.
function backspace () {
  // Check the last character in the result.
  // If the result is a single zero, do NOT remove.
  if ($("#result").text() != "0") {
    $("#result").text($("#result").text().slice(0,$("#result").text().length - 1));
  }
  
  if ($("#result").text() === "") {
    $("#result").text("0");
  }
}

// Clear the result.
function clearResult() {
  $("#result").text("");
}

// Clear the log.
function clearLog() {
  $("#log").text("");
}

// Count the number of digits in the display, excluding the decimal point and the negative sign.
function countResult() {
  return $("#result").text().match(/[0-9]/g) ? $("#result").text().match(/[0-9]/g).length:0;
}

// This function compares the given string with the four basic operations.
// It returns true if it matches any of them, and false if it doesn't.
function isOperation(str) {
  return str === "+" || str === "-" || str === "*" || str === "/";
}

              
            
!
999px

Console