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

              
                <!-- THis is HTML for the FCC calculator-->
  <div class="document">
    <div class="calculator-wrapper">
      <div class="calculator">
        <div class="branding">
          <div class="brand">FCC</div>
          <div class="description">Electronic calculator</div>
          <div class="model">HAL-42</div>
        </div>
        <div class="screen-frame">
          <div class="screen">
            <div class="screen-background">-8.8.8.8.8.8.8.8.</div>
            <div class="screen-text">0</div>
            <div class="memory-sign-background">M</div>
            <div class="memory-sign">M</div>
          </div>
        </div>
        <div class="buttons">
          <table class="top-row">
            <tr>
              <td>
                <div class="button small memory" id="MRC">MRC</div>
              </td>
              <td>
                <div class="button small memory" id="Mplu">M+</div>
              </td>
              <td>
                <div class="button small memory" id="Mmin">M-</div>
              </td>
              <td>
                <div class="button small sqrt" id="sqrt">&radic;</div>
              </td>
              <td>
                <div class="button small off" id="OFF">OFF</div>
              </td>
            </tr>
          </table>
          <table class="other-rows">
            <tr>
              <td>
                <div class="button red clear on" id="AC">AC</div>
              </td>
              <td>
                <div class="button red clear" id="CE">C</div>
              </td>
              <td>
                <div class="button percent" id="percent">%</div>
              </td>
              <td>
                <div class="button plusmn" id="plusmn">&plusmn;</div>
              </td>
            </tr>
            <tr>
              <td>
                <div class="button digit" id="d7">7</div>
              </td>
              <td>
                <div class="button digit" id="d8">8</div>
              </td>
              <td>
                <div class="button digit" id="d9">9</div>
              </td>
              <td>
                <div class="button operator" id="div">/</div>
              </td>
            </tr>
            <tr>
              <td>
                <div class="button digit" id="d4">4</div>
              </td>
              <td>
                <div class="button digit" id="d5">5</div>
              </td>
              <td>
                <div class="button digit" id="d6">6</div>
              </td>
              <td>
                <div class="button operator" id="mul">&times;</div>
              </td>
            </tr>
            <tr>
              <td>
                <div class="button digit" id="d1">1</div>
              </td>
              <td>
                <div class="button digit" id="d2">2</div>
              </td>
              <td>
                <div class="button digit" id="d3">3</div>
              </td>
              <td>
                <div class="button operator" id="min">&ndash;</div>
              </td>
            </tr>
            <tr>
              <td>
                <div class="button digit" id="d0">0</div>
              </td>
              <td>
                <div class="button dot" id="dot">.</div>
              </td>
              <td>
                <div class="button equal" id="equal">=</div>
              </td>
              <td>
                <div class="button operator" id="plu">+</div>
              </td>
            </tr>
          </table>
        </div>
      </div>
    </div><div class="text-wrapper">
      <div class="text">
        <h1>Congratulations on choosing the HAL-42.</h1>
        <h2>The latest calculator by <em>FCC</em></h2>
        <p>Equiped with the most advanced functionalities:</p>
        <lu>
          <li><strong>Percent calculation:</strong> For example, to calculate 10% off £120, simply type <code>1 2 0 - 1 0 % =</code></li>
          <li><strong>Easy squaring:</strong> For example, to calculate the square of 234, simply type <code> 2 3 4 &times; &times;</code></li>
          <li><strong>Compound interests:</strong> For example, to see how much you'll have in an account with a 5% interest when you deposit £2100, simply type <code> 1 . 0 5 &times; &times; =</code>. Ignore the result and type <code>2 1 0 0 =</code>. Pressing <code>=</code> again gives you the interest for each subsequent year.</li>
          <li><strong>And many more!</strong></li>
        </lu>
        <p><strong>To turn it on</strong>, press the <code>AC</code> button.</p>
        <p>You can interact with the calculator using the mouse or the keyboard. Hover on each key to discover its keyboard shortcut.</p>

        <p>Design inspired by the <a href="http://www.casio-intl.com/asia-mea/en/calc/practical/portable/product_list/?m_no=120" target="_blank">Casio HL-4</a>.</p>

        <p>7-segment font by <a href="http://www.keshikan.net/fonts-e.html" target="_blank">keshikan-zume</a>.</p>
      </div>
    </div>
  </div>

              
            
!

CSS

              
                /* This is CSS for the FCC calculator */
/* Font from http://www.keshikan.net/fonts-e.html */
@font-face {
  font-family: 'Segments';
  src: url('https://raw.githubusercontent.com/jpdrecourt/FCC-Calculator/master/font/DSEG7Classic-Italic.woff');
}

body {
  background-color: #674970;
  color: #dbe9ee;
  font-family: sans-serif;;
  margin: 10px 0 0 0;
  padding: 0;
}

.document {
  max-width: 950px;
  margin: 0 auto;
}

@media (min-width: 950px) {
  .calculator-wrapper {
    display: inline-block;
    padding: 50px;
  }
  .text-wrapper {
    display: inline-block;
    vertical-align: top;
    width : 475px;
  }
}

@media (max-width: 949px) {
  .calculator-wrapper {
    padding: 50px auto;
  }
  .text-wrapper {
    padding: 50px auto;
  }

}


.calculator {
  background-color: #445565;
  box-shadow: 0 6px 10px 0 rgba(0, 0, 0, 0.2), 0 10px 24px 0 rgba(0, 0, 0, 0.19);
  border-radius: 10%;
  margin: auto;
  width: 375px;
}

.text {
  margin: auto;
  padding-top: 50px;
  width: 375px;
  font-size: 16px;
}

.text h1 {
  margin-top: 0;
}

.text p,
.text li {
  line-height: 1.4em;
  padding-bottom: 5px;
}

.text a {
  color: inherit;
}

.text code {
  border-radius: 3px;
  padding: 0 3px;
  background-color: rgba(255, 255, 255, 0.11);
}

.branding {
  color: #b9c3c9;
  font-family: sans-serif;
  height: 95px;
  padding-top: 20px;
  text-align: center;
}

.brand {
  font-size: 20px;
  font-weight:900;
  letter-spacing: 1px;
  margin: 0;
}

.description {
  font-size: 12px;
  font-weight: 900;
  margin-top: 20px;
  text-transform: uppercase;
}

.model {
  font-size: 14px;
  font-weight: 900;
  margin-top: 3px;
}

.screen-frame {
  background-color: #5a6c77;
  border-top-right-radius: 10px;
  border-top-left-radius: 10px;
  border-bottom-right-radius: 30%;
  border-bottom-left-radius: 30%;
  border: solid 1px #5a6c77;
  height: 110px;
  margin: 0 auto;
  width: 320px;
}

.screen {
  background-color: #aacfb8;
  border-radius: 5px;
  position: relative;
  height: 75px;
  width: 250px;
  margin: 13px auto auto auto;
  font-size: 32px;
}

/* Common properties for overlay */
.screen-background,
.screen-text {
  font-family: 'Segments', Fallback, monospace;
  position: absolute;
  top: 25px;
  right: 10px;
}

.screen-background {
  color: black;
  opacity: 0.15;
}

.screen-text {
  color: black;
  visibility: hidden;
}

/* Common properties for overlay */
.memory-sign-background,
.memory-sign {
  color: black;
  font-family: sans-serif;
  font-size: 15px;
  font-weight: bold;
  line-height: 100%;
  position: absolute;
  position: absolute;
  right: 10px;
  top: 5px;
}

.memory-sign-background {
  visibility: visible;
  opacity: 0.15;
}

.memory-sign {
  visibility: hidden;
}

td,
tr,
table {
  text-align: center;
  margin: 0;
  padding: 0;
}

.top-row {
  width: 100%;
}

.other-rows {
  width: 100%;
}

.buttons {
  margin: 20px 30px 15px 30px;
}

.button {
  border-radius: 7px;
  font-family: sans-serif;
  background-color: #0a2637;
  color: #cddde3;
  display: inline-block;
  height: 35px;
  font-weight: bold;
  font-size: 25px;
  line-height: 35px;
  margin-bottom: 15px;
  position: relative;
  text-align: center;
  width: 55px;
}

.button.small {
  font-size: 18px;
  height: 38px;
  line-height: 38px;
  margin-bottom: 16px;
  width: 50px;
}

.button.red {
  background-color: #d42828;
}

.button:hover {
  opacity: 0.8;
}

.tooltip {
  font-size: 8px;
  line-height: 100%;
  position: absolute;
  right: 2px;
  bottom: 2px;
  visibility: hidden;
  z-index: 1;
}
.button:hover {
  cursor: pointer;
}

.button:hover .tooltip {
    visibility: visible;
}

.active,
.button:active,
.button.small:active {
  opacity: 0.5;
}

              
            
!

JS

              
                // This is the Javascript for the FCC calculator
'use strict';

var previousKey = "", // Prevent keyboard repeat
    keyIdTable = {"q": "MRC", "Q": "MRC",
                  "w": "Mplu", "W": "Mplu",
                  "e": "Mmin", "E": "Mmin",
                  "r": "sqrt", "R": "sqrt",
                  "o": "OFF", "O": "OFF",
                  "Delete": "CE", "Backspace": "CE",
                  "Escape": "AC",
                  "%": "percent",
                  "n": "plusmn", "N": "plusmn",
                  "0": "d0", "1": "d1", "2": "d2", "3": "d3", "4": "d4",
                  "5": "d5", "6": "d6", "7": "d7", "8": "d8", "9": "d9",
                  "/": "div", "*": "mul", "-": "min", "+": "plu",
                  ".": "dot",
                  "=": "equal", "Enter": "equal"},
    displaySize = 8,
    $screenText = $(".screen-text"),
    operator = "",
    firstOperand = "",
    lastPressed = "", // Values can be "digit", "operator", "result", "MRC", "unary"
    isOverflow = false,
    memory = 0,
    isOn = false;

// Keyboard handler
$(document).keydown(function(event) {
  if (event.key != "F5"){ //DEBUG
    event.preventDefault();
  }
  // Prevent repetitive key strokes
  if (previousKey == event.key){
    return;
  }
  previousKey = event.key;

  // Blinking of the appropriate key & Process
  $keyToId(event.key)
    .blink()
    .processInput();
});

$(document).keyup(function() {
  previousKey = ""; // Allows multiple keys pressed at the same time
})

$(".calculator").click(function(event) {
  $(event.target).processInput();
})

$(document).ready(function() {
  addToolTips();
})

// Blinks the key when pressed
$.fn.blink = function() {
  this.addClass("active")
      .delay(100)
      .queue(function() {
        $(this).removeClass("active").dequeue();
      });
  return this;
};

// Main input handler
$.fn.processInput = function() {
  var buttonId = this.attr("id");
  // Block all entries if off
  if (!isOn && !this.hasClass('on')){
    return;
  }
  // Parse CE and C
  if (this.hasClass('clear')) {
    parseClear(buttonId);
    return;
  }
  if (!isOverflow && isOn) {
    // Parse decimal dot
    if (this.hasClass('dot')) {
      parseDot();
      return;
    }
    // Parse sign change
    if (this.hasClass('plusmn')) {
      parsePlusmn();
      return;
    }
    // Parse digits
    if (this.hasClass('digit')) {
      parseDigit(buttonId);
      return;
    }
    if (this.hasClass('operator')) {
      parseOperator(buttonId);
      return;
    }
    if (this.hasClass('equal')) {
      parseEqual();
      return;
    }
    if (this.hasClass('memory')) {
      parseMemory(buttonId);
      return;
    }
    if (this.hasClass('percent')) {
      parsePercent();
      return;
    }
    if (this.hasClass('sqrt')) {
      parseSqrt();
      return;
    }
    if (this.hasClass('off')) {
      parseOff();
      return;
    }
  }
}

////////////Parsing functions///////////////////
var parseClear = function(id) {
  $screenText.text("0");
  if (id == "AC") {
    if (isOn) {
      operator = "";
      firstOperand = "";
      lastPressed = "";
    } else {
      // Turn on
      isOn = true;
      $screenText.css({"visibility": "visible"});
    }
  }
  isOverflow = false;
}

var parseDot = function() {
  if (lastPressed != "digit") {
    parseClear("CE");
  }
  if ($screenText.text().indexOf('.') == -1) {
    $screenText.append('.');
  }
  lastPressed = "digit";
}

var parseDigit = function(id) {
  if (lastPressed == "operator" || (lastPressed == "result" && operator[0] == 2)) {
    parseClear("CE");
  } else if (lastPressed == "result" || lastPressed == "MRC" || lastPressed == "unary") {
    parseClear("AC");
  }
  // Handle the original zero
  if ($screenText.text() == "0") {
    $screenText.text(id.slice(-1));
  } else if ($screenText.text().match(/[0-9]/g).length < 8) {
    // If less than 8 digits, display the digits
    $screenText.append(id.slice(-1));
  }
  lastPressed = "digit";
}

var parsePlusmn = function() {
  if (lastPressed == "operator") {
    operator = "";
  }
  var screenText = clearTrailingZeroes($screenText.text());
  if (parseFloat(screenText) !== 0) {
    if (screenText.slice(0, 1) == "-") {
      $screenText.text(screenText.slice(1));
    } else {
      $screenText.text("-" + screenText);
    }
  }
  // No need to update lastPressed
}

var parseOperator = function(id) {
  $screenText.text(clearTrailingZeroes($screenText.text()));
  if (lastPressed == "operator") {
    // Possible repeat operation
    if (operator.slice(-3) == id) { // In case it's already the repeat
      operator = "2" + id;
      firstOperand = $screenText.text();
      $screenText.text(calculate());
      lastPressed = "result";
    } else {
      // We've just changed the operator
      operator = id;
      lastPressed = "operator";
    }
  } else {
    if (firstOperand == "" || operator[0] == "2") {
      //  First operation or clear repeat operation
      firstOperand = $screenText.text();
    } else {
      // More than one operation - Calculate
      $screenText.text(calculate());
      firstOperand = $screenText.text();
    }
    operator = id;
    lastPressed = "operator";
  }
}

var parseEqual = function() {
  switch (lastPressed) {
    case "digit":
    case ".":
    case "unary":
      if (operator == "") {
        $screenText.text(clearTrailingZeroes($screenText.text()));
      } else {
        $screenText.text(calculate());
      }
      break;
    case "result":
      if (operator[0] == "2"){
        // Repeat operation
        $screenText.text(calculate());
      }
      break;
    default:
      $screenText.text(clearTrailingZeroes($screenText.text()));
  }
  if (operator[0] != 2) {
    // Only forget if it's not a double operator
    operator = "";
    firstOperand = "";
  }
  lastPressed = "result";
}

var parseMemory = function(id) {
  switch (id) {
    case "Mplu":
    case "Mmin":
      var newVal = operatorFn(id.slice(-3))(memory,
                                            parseFloat($screenText.text()));
      checkForOverflow(newVal);
      if (isOverflow) {
        displayError();
      } else {
        memory = newVal;
      }
      lastPressed = "result";
      break;
    case "MRC":
      if (lastPressed == "MRC") {
        memory = 0;
        lastPressed = "result";
      } else {
        parseClear("AC");
        $screenText.text(resultToText(memory));
        lastPressed = "MRC";
      }
      break;
  }
  if (memory === 0) {
    $(".memory-sign").css({"visibility": "hidden"});
  } else {
    $(".memory-sign").css({"visibility": "visible"});
  }
}

var parsePercent = function() {
  var value = parseFloat($screenText.text());
  if (firstOperand == "") {
    value = value * 0.01;
  } else {
    value = value * 0.01 * parseFloat(firstOperand);
  }
  $screenText.text(resultToText(value));
  lastPressed = "unary";
}

var parseSqrt = function() {
  $screenText.text(resultToText(Math.sqrt(parseFloat($screenText.text()))));
  lastPressed = "unary";
}

var parseOff = function() {
  $screenText.css({"visibility": "hidden"});
  $(".memory-sign").css({"visibility": "hidden"});
  $screenText.text("");
  operator = "";
  firstOperand = "";
  lastPressed = "";
  isOverflow = false;
  memory = 0;
  isOn = false;
}
//////////End of parsing functions

// Process operands and operator
var calculate = function() {
  var secondOperand = $screenText.text();
  var result = operatorFn(operator.slice(-3))(parseFloat(firstOperand),
                                              parseFloat(secondOperand));
  checkForOverflow(result);
  return resultToText(result);
}

var operatorFn = function(operator) {
  switch (operator) { // Takes into account double operator
    case "plu":
      return function (lhs, rhs) {return lhs + rhs};
      break;
    case "min":
      return function (lhs, rhs) {return lhs - rhs};
      break;
    case "mul":
      return function (lhs, rhs) {return lhs * rhs};
      break;
    case "div":
      return function (lhs, rhs) {return lhs / rhs}
      break;
    default:
      return;
  }
}

// Display an E at the end of the display
var displayError = function() {
  var text = $screenText.text();
  $screenText.text(text.slice(0, -1) + "E");
}

// Convert result to text and deals with overflow
var resultToText = function(value) {
  var text = "";
  if (isOverflow) {
    // Handle NaN
    if (isNaN(value)) {
      text = "E";
    } else if (isFinite(value)) {
      // Finite but too big
      text = "" + Math.abs(value);
      text = text.slice(0, 1) + "." + text.slice(1, 7) + "E";
    } else {
      // Infinity
      text = "9.999999E";
    }
    if (value < 0) {
      text = "-" + text;
    }
  } else {
    // OK value - Trim extra digits
    var nDecimals = Math.min(displaySize - Math.log10(Math.abs(value)), 7);
    text = value.toFixed(nDecimals);
  }
  return clearTrailingZeroes(text);
}

var clearTrailingZeroes = function(text) {
  if (text.indexOf('.') != -1) {
    while (text.slice(-1) == "0") {
      text = text.slice(0, -1);
    }
    if (text.slice(-1) == ".") {
      text = text.slice(0, -1);
    }
  }
  return text;
}

var checkForOverflow = function(value) {
  isOverflow = (value > 99999999 || value < -99999999 || isNaN(value));
}

// Returns the ID of the element called by the keyboard shortcut
var $keyToId = function(key) {
  return $('#' + keyIdTable[key]);
}

// Adds the keyboard tooltips
var addToolTips = function() {
  var idKeyTable = createTooltips(keyIdTable);
  for (var id in idKeyTable) {
    $("#" + id).append("<span class='tooltip'>" + idKeyTable[id] + "</span>");
  }
}

// Reverts key/value relationships. Handles multiple values and lower case single keys
var createTooltips = function(object) {
  var out = {};
  var v;
  for(var k in object) {
    v = object[k];
    if (v.length == 1) { // Keeps "Escape", etc. upper case
      v = v.toLowerCase();
    }
    if (out[v] === undefined) {
      out[v] = k.slice(0, 3);
    }
  }
  return out;
}

              
            
!
999px

Console