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 id="calculator">
  <div id="display"> </div>
  <div class="keypad">
    <div class="numpad">
      <button class="btn num" id="one" onclick="updateCurrentNum(1)"> 1 </button>
      <button class="btn num" id="two" onclick="updateCurrentNum(2)"> 2 </button>
      <button class="btn num" id="three" onclick="updateCurrentNum(3)"> 3 </button>
      <button class="btn num" id="four" onclick="updateCurrentNum(4)"> 4 </button>
      <button class="btn num" id="five" onclick="updateCurrentNum(5)"> 5 </button>
      <button class="btn num" id="six" onclick="updateCurrentNum(6)"> 6 </button>
      <button class="btn num" id="seven" onclick="updateCurrentNum(7)"> 7 </button>
      <button class="btn num" id="eight" onclick="updateCurrentNum(8)"> 8 </button>
      <button class="btn num" id="nine" onclick="updateCurrentNum(9)"> 9 </button>
      <button class="btn cmd" id="decimal" onclick="insertDecimal()"> . </button>
      <button class="btn num" id="zero" onclick="updateCurrentNum(0)"> 0 </button>
      <button class="btn cmd" id="equals" onclick="showAnswer()"> = </button>
    </div>
    <div class="ops">
      <button class="btn cmd" id="add" onclick="changeOp('+')"> + </button>
      <button class="btn cmd" id="subtract" onclick="changeOp('-')"> - </button>
      <button class="btn cmd" id="multiply" onclick="changeOp('x')"> x </button>
      <button class="btn cmd" id="divide" onclick="changeOp('/')"> / </button>
      <button class="btn" id="clear" onclick="reset()"> Clear</button>
    </div>
  </div>
</div>
              
            
!

CSS

              
                body {
  font-family: sans-serif;
}

#calculator {
  width: 300px;
  margin-left: auto;
  margin-right: auto;
  background: #880099;
  border-radius: 10px;
  border: 4px solid #009977;
  padding-bottom: 50px;
  box-shadow: 3px 3px #444;
}

#display {
  background: #fff;
  font-size: 36px;
  text-align: right;
  margin-top: 15px;
  border-radius: 5px;
  width: 90%;
  margin-left: auto;
  margin-right: auto;
  border: 2px solid #009977;
}
.keypad {
  display: flex;
  width: 70%;
  margin-left: auto;
  margin-right: auto;
  margin-top: 5px;
}

.btn {
  font-size: 20px;
  margin: 2px;
  text-align: center;
}

.numpad .btn {
  background: #009977;
  color: #fff;
  text-shadow: 1px 1px #444;
  width: 20%;
}

.numpad .cmd {
  background: #fff;
  color: #000;
  text-shadow: 0px 0px;
}

.ops .cmd {
  width: 40%;
}

#sqrt, #clear {
  width: 90%;
}

              
            
!

JS

              
                var current = {};
window.onload = function() {
  reset();
};

function reset() {
  current = {
    total: 0,
    operation: "",
    input: 0,
    digits: 0,
    decPlaces: 0, // decimal place of *next* number to be entered
    doubleOp: false
  };
  $("#display").html(current.input);
}

function updateCurrentNum(content) {
  if (current.decPlaces) {
    updateDecimal(content);
  } else if (current.digits == 0) {
    current.digits = 1;
    current.input = content;
  } else {
    nextDigit(content);
  }
  current.doubleOp = false;
  if (current.decPlaces !== 0) {
    var asDec = makeFixed(current.input, current.decPlaces - 1);
    $("#display").html(asDec);
    return;
  }
  $("#display").html(current.input);
}

function makeFixed(number, places) {
  return (Math.round(number * 10 ** places) / 10 ** places).toFixed(places);
}
function numDisplayed() {
  var display = $("#display").html();
  var numeric = /[0-9]/;
  return numeric.test(display);
}

function decDisplayed() {
  var display = $("#display").html();
  var dec = /\./;
  return dec.test(display);
}
function insertDecimal() {
  if (decDisplayed()) {
    console.log("double decimal");
  } else if (numDisplayed()) {
    current.decPlaces = 1;
    $("#display").append(".");
  } else {
    current.decPlaces = 1;
    $("#display").html("0.");
  }
}
function updateDecimal(content) {
  console.log(10 ** current.decPlaces);
  content /= 10 ** current.decPlaces;
  current.input += content;
  current.decPlaces += 1;
}

function nextDigit(digit) {
  current.digits += 1;
  current.input *= 10;
  current.input += digit;
}

function changeOp(op) {
  if (current.operation && !current.doubleOp) {
    operate();
  } else if (current.doubleOp) {
    console.log("Double Op");
  } else {
    current.total = current.input;
  }
  current.operation = op;
  current.doubleOp = true;
  current.input = 0;
  current.digits = 0;
  current.decPlaces = 0;
  $("#display").html(op);
}

function operate() {
  if (current.operation == "+") {
    current.total += current.input;
  } else if (current.operation == "-") {
    current.total -= current.input;
  } else if (current.operation == "x") {
    current.total *= current.input;
  } else if (current.operation == "/") {
    current.total /= current.input;
  }
  console.log("=" + current.total);
}

function showAnswer() {
  operate();
  console.log(current.total % 1);
  if (!current.operation) {
    current.total = current.input;
  }
  if (current.decPlaces !== 0) {
    current.total = makeFixed(current.total, current.decPlaces - 1);
  }

  current.operation = "";
  current.input = current.total;
  current.doubleOp = false;
  $("#display").html(current.total);
}

              
            
!
999px

Console