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

              
                <body>
  <div class="container center-align">
    <div id="calculator">
      <div class="card grey lighten-2">
        <div class="card-content center-align">
          <div id="solar" class="right-align">
            <div class="solarcell">&nbsp;</div>
            <div class="solarcell">&nbsp;</div>
            <div class="solarcell">&nbsp;</div>
            <div class="solarcell">&nbsp;</div>
            <div class="solarcell">&nbsp;</div>
          </div>
          <h5 class="left-align white-text">Cakkalate</h5>
          <div class="row">
            <div id=displayContainer class="col s12 right-align">
              <h3 id="display" class="lime lighten-4">0</h3>
            </div>
          </div>
          <div class="row">
            <div class="col s3">
              <a id="Escape" class="btn waves-effect yellow lighten-5 grey-text text-darken-4">ac</a>
            </div>
            <div class="col s3">
              <a id="Backspace" class="waves-effect btn yellow lighten-5 grey-text text-darken-4">c</a>
            </div>
            <div class="col s3">
              <a id="percent" class="waves-effect btn yellow lighten-5 grey-text text-darken-4">%</a>
            </div>
            <div class="col s3">
              <a id="divide" class="operator waves-effect btn yellow lighten-5 grey-text text-darken-4">÷</a>
            </div>
          </div>
          <div class="row">
            <div class="col s3">
              <a id="7" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">7</a>
            </div>
            <div class="col s3">
              <a id="8" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">8</a>
            </div>
            <div class="col s3">
              <a id="9" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">9</a>
            </div>
            <div class="col s3">
              <a id="multiply" class="operator waves-effect btn yellow lighten-5 grey-text text-darken-4">x</a>
            </div>
          </div>
          <div class="row">
            <div class="col s3">
              <a id="4" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">4</a>
            </div>
            <div class="col s3">
              <a id="5" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">5</a>
            </div>
            <div class="col s3">
              <a id="6" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">6</a>
            </div>
            <div class="col s3">
              <a id="subtract" class="operator waves-effect btn yellow lighten-5 grey-text text-darken-4">-</a>
            </div>
          </div>
          <div class="row">
            <div class="col s3">
              <a id="1" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">1</a>
            </div>
            <div class="col s3">
              <a id="2" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">2</a>
            </div>
            <div class="col s3">
              <a id="3" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">3</a>
            </div>
            <div class="col s3">
              <a id="add" class="operator waves-effect btn yellow lighten-5 grey-text text-darken-4">+</a>
            </div>
          </div>
          <div class="row">
            <div class="col s3">
              <a id="0" class="num waves-effect btn yellow lighten-5 grey-text text-darken-4">0</a>
            </div>
            <div class="col s3">
              <a id="dot" class="waves-effect btn yellow lighten-5 grey-text text-darken-4">.</a>
            </div>
            <div class="col s6">
              <a id="Enter" class="waves-effect btn yellow lighten-5 grey-text text-darken-4">=</a>
            </div>
          </div>

        </div>
      </div>
    </div>
  </div>
</body>
              
            
!

CSS

              
                /* To do: alter the position of the decimal, or try another font */
body {
  background-color: darkslategray;
}

.container {
  width: 320px;
  max-height: 470px;
}

.card {
/*   border: 2px outset gray; */
  width: 320px;
  max-height: 470px;
  margin-bottom: 0px;
  border-radius: 5px;
  box-shadow:
    inset 0px 0px 8px rgba(0,0,0,.7),
    3px 3px 10px rgba(0,0,0,0.2);
}

.card-content {
  height: 470px;
}

#solar {
  width: 280px;
  height: 40px;
  padding: 5px;
  margin-bottom: 0px;
  background-color: #996e00;
}

.solarcell {
  display: inline-block;
  height: 25px;
  width: 20px;
/*   margin: 0px;
  padding: 0px; */
  background-color: #4d3333;
  border: 2px solid #555;
  
}

h3 {
  font-family: 'Segment7Standard', monospace; 
  font-weight: normal; 
  font-style: italic;
  border: 6px inset #CCC;
  padding-right: 2px;
  margin-top: 0px;
}

h5 {
  width: 100%;
  margin-bottom: 0px !important;
  margin-top: 0px;
  background-color: #e6a400;
  padding: 5px;
}

.btn {
  width: 100% !important;
/*   background-color: lightyellow;
  color: black; */
}
              
            
!

JS

              
                window.onkeydown = function(e) {
  var keyID;
  switch (e.key) { // .key returns the "printed" character, so top-row and num pad digits are the same. In the html, the IDs of number buttons are the number in question. However, for clarity and because jQuery can't handle non-alphanumeric IDs, the operators and function buttons are given word IDs, assigned below.
    case "%":
      keyID = "percent";
      break;
    case ".":
      keyID = "dot";
      break;
    case "/":
      keyID = "divide";
      break;
    case "*":
      keyID = "multiply";
      break;
    case "-":
      keyID = "subtract";
      break;
    case "+":
      keyID = "add";
      break;
    default:
      keyID = e.key;
  }
  $("#" + keyID).click();
}

var valDone = true; //This, set to false, will translate to "we're in the middle of inputting a number here!"
var equationDone = true; //This, set to false, will translate to "we're in the middle of an equation here!"
var currentVal = 0;
var holdingVal = 0;
var currentOperator = "";

function operation(a, b) {
  if (currentOperator === "divide") {
    if (b === 0) { //throw error on divide by 0
      $("#display").html("E&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
      setTimeout(escape, 1500); //escape is the function activated by the "AC" key, defined below
    } else {
      return +(a / b).toFixed(10); //fixing the result to 10 decimal places (max display can accomodate) fixes peculiarities of 64-bit representation of floating point decimals. The leading "+" turns the string back into a number.
    }
  } else if (currentOperator === "multiply") {
    return +(a * b).toFixed(10);
  } else if (currentOperator === "subtract") {
    return +(a - b).toFixed(10);
  } else { //if the current operator "", meaning the start of an equation, we still want to add the currentVal to holdingVal
    return +(a + b).toFixed(10);
  }
}

// To emulate the action of a physical calculator, and to give some feedback on pressing an operator button for the first time (not changing the displayed number) I introduced this function  to momentarily hide and then show the text. Rather than use a .hide, .show, or add and remove a visibility or opacity class (I wanted to keep the element's bgcolor), I just made the text color match the background.
var blink = function() {
  $("#display").addClass("lime-text text-lighten-4");
  setTimeout("$('#display').removeClass('lime-text text-lighten-4');", 100);
}

// I want different actions depending on whether the display is FULL vs an equation returning an overflowing value
var displayFull = function() {
  // The decimal doesn't count in the "character count"
  if ($("#display").text().replace('.', '').length === 11) {
    return true;
  } else {
    return false;
  }
}

function checkOverflow() {
  if ($("#display").text().replace('.', '').length <= 11) {
    return;
  } else {
    // display "E" at the far left of the display, like early calculators
    $("#display").html("E&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
    setTimeout(escape, 1500);
  }
}

var checkSpelling = function() {
  switch ($("#display").text()) {
    case "0.7734":
      Materialize.toast("Well hello to you to!", 4000);
      break;
    case "5318008":
      Materialize.toast("Seriously? Classy.", 4000);
      break;
    case "53045.3080": // http://hrwiki.org/wiki/Cakkalate
      Materialize.toast("\"Look! I can make mine say... oboe shoes!\"", 4000);
      break;
    default:
      return;
  }
}

$(".num").click(function() {
  // are we starting a number from scratch, or adding digits to one?
  if (valDone === true || $("#display").text() === "0") { //Why both? You could have just finished an operation, leaving a number on the screen, and be starting to input a new number, or you could be showing "0" from having backspaced. OR you could be a moron and trying to start your number by pressing 0 a bunch of times. Moron. Saw you comin.
    blink();
    $("#display").text(this.id);
  } else if (displayFull()) {
    blink(); // and do nuttin
  } else {
    blink();
    $("#display").append(this.id);
  }
  currentVal = Number($("#display").text());
  valDone = false;
  checkSpelling();
});

var escape = function() { //a named function, so the overflow error function can use it too
  currentVal = 0;
  holdingVal = 0;
  blink();
  $("#display").text(currentVal);
  valDone = true;
  equationDone = true;
}

$("#Escape").click(escape);

$("#Backspace").click(function() {
  currentVal = Number($("#display").text().slice(0, -1));
  blink();
  $("#display").text(currentVal);
  if (currentVal === 0) {
    valDone = true;
  }
});

$("#dot").click(function() {
  if (valDone === true) {
    blink();
    $("#display").text("0.");
  } else if (displayFull()) {
    blink();
  } else {
    currentVal = $("#display").text();
    currentVal += ".";
    blink();
    $("#display").text(currentVal);
  }
  valDone = false;
});

$(".operator").click(function() {
  if (equationDone === true) {
    var result = currentVal;
  } else {
    result = operation(holdingVal, currentVal);
  }
  currentVal = result;
  holdingVal = result;
  currentOperator = this.id;
  blink();
  $("#display").text(currentVal);
  checkOverflow();
  equationDone = false;
  valDone = true;
});

$("#Enter").click(function() {
  var result = operation(holdingVal, currentVal);
  currentVal = result;
  holdingVal = result;
  currentOperator = "";
  blink();
  $("#display").text(currentVal);
  checkOverflow();
  valDone = true;
  equationDone = true;
});

// What does the percent key DO anyway? see https://blogs.msdn.microsoft.com/oldnewthing/20080110-00/?p=23853
$("#percent").click(function() {
  var percentage = holdingVal * currentVal / 100;
  currentVal = percentage;
  blink();
  $("#display").text(currentVal);
  checkOverflow();
});
              
            
!
999px

Console