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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div class="calculator">
  <div class="display" id="disp"></div>
  <div class="titel">Electronic calculator</div>
  <div class="button S" id="b7">7</div>
  <div class="button S" id="b8">8</div>
  <div class="button S" id="b9">9</div>
  <div class="button S" id="bN">+/-</div>
  <div class="button S" id="bC">C</div>
  <div class="button S" id="b4">4</div>
  <div class="button S" id="b5">5</div>
  <div class="button S" id="b6">6</div>
  <div class="button S" id="bD">÷</div>
  <div class="button S" id="bM">x</div>
  <div class="button S" id="b1">1</div>
  <div class="button S" id="b2">2</div>
  <div class="button S" id="b3">3</div>
  <div class="button S" id="bS">-</div>
  <div class="button S" id="bA">+</div>
  <div class="button R" id="b0">0</div>
  <div class="button S" id="bF">.</div>
  <div class="button S" id="bP">^</div>
  <div class="button S" id="bE">=</div>
</div>
              
            
!

CSS

              
                body {
  background-image: url("http://sf.uploads.ru/d/tCfZq.jpg");
}

.calculator {
  width: 305px;
  height: 360px;
  margin-left: auto;
  margin-right: auto;
  margin-top: 50px;
  margin-bottom: 50px;
  padding-top: 10px;
  padding-left: 10px;
  padding-right: 10px;
  border-radius: 30px;
  border-style: groove;
  border-width: 3px;
  border-color: lightblue;
  background-color: lightblue;
  box-shadow: 30px 30px 30px 0 grey;
}

.display {
  width: 245px;
  height: 40px;
  line-height: 40px;
  margin-left: auto;
  margin-right: auto;
  margin-top: 15px;
  margin-bottom: 15px;
  text-align: right;
  padding-left: 15px;
  padding-right: 15px;
  border-radius: 15px;
  border-style: inset;
  border-width: 3px;
  background-color: lightgrey;
  font-family: 'Roboto Mono', monospace;
  font-size: 1.5em;
  color: green;
}

.titel {
  width: 350px;
  height: 25px;
  line-height: 25px;
  margin-left: 13px;
  margin-right: auto;
  margin-top: 5px;
  margin-bottom: 5px;
//  padding: 5px;
  background-color: transparent;
  user-select: none;
  font-family: 'Tangerine', cursive;
  font-size: 2em;
}

.button {
  font-family: 'Anonymous Pro', monospace;
//  font-weight: bold;
  line-height: 35px;
  text-align: center;
  margin: 9px;
  float:left;
  padding: 0;
  border-radius: 15px;
  border-style: outset;
  border-width: 4px;
  background-color: lightgrey;
  user-select: none;
}

.button:active {
  border-style: inset;
}

.S {
  width: 35px;
  height: 35px;
}

.R {
  width: 96px;
  height: 35px;
}

              
            
!

JS

              
                var display;
var result;
var operation;
var sign;
var newNumber;
var stopEnter;

function resetCalculator() {
  display = "0";
  result = 0;
  operation = "";
  sign = "";
  newNumber = true;
  stopEnter = false;
  disp.innerHTML = sign+display;
}

function buttonType(b) {
  switch (b) {
    case "0":
    case "1":
    case "2":
    case "3":
    case "4":
    case "5":
    case "6":
    case "7":
    case "8":
    case "9":
      return "number";
    case ".":
      return "dot";
    case "+":
    case "-":
    case "*":
    case "/":
    case "^":
      return "operation";
    case "=":
      return "equal";
    case "n":
      return "negate";
    case "c":
      return "reset";
    default:
      return "invalid";
  }
}

function performOperation(op, op1, op2) {
  var result;
  op1 = Number(op1);
  op2 = Number(op2);
  console.log("op = " + op);
  console.log("op1 = " + op1);
  console.log("op2 = " + op2);
  switch (op) {
    case "+":
      result = op1 + op2;
      break;
    case "-":
      result = op1 - op2;
      break;
    case "*":
      result = op1 * op2;
      break;
    case "/":
      result = op1 / op2;
      break;
    case "^":
      result = Math.pow(op1, op2);
      break;
    default:
      result = NaN;
  }
  result = String(result);
  if (result == "NaN" || result == "Infinity" || result == "-Infinity") {
    result = "Error";
    stopEnter = true;
  }
  if ((result.indexOf("e") != -1) || (result.indexOf(".") > 15)) {
    result = "Overflow";
    stopEnter = true;
  }
  else 
    result = result.substr(0, 17);
  
  return result;
}

function processButton(b) {
  console.log("processButton(" + b + ")");
  console.log("\tdisplay = " + display);
  console.log("\tsign = " + sign);
  console.log("\tresult = " + result);
  console.log("\toperation = " + operation);
  console.log("\tnewNumber = " + newNumber);
  console.log("\tstopEnter = " + stopEnter);
  if (!stopEnter) {
    if (newNumber) {
//      sign = "";
      if (buttonType(b) == "number" /* && b != "0"*/) {
        sign = "";
        display = b;
        newNumber = false;
      }
      if (buttonType(b) == "dot") {
        sign = "";
        display = "0.";
        newNumber = false;
      }
    } else {
      if (buttonType(b) == "number" && display != "0") display += b;
      if (buttonType(b) == "dot" && display.indexOf(".") == -1)
        display += b;
    }

    if (buttonType(b) == "negate") {
      if (sign == "-")
        sign = "";
      else
        sign = "-";
/*    if (display[0] != "-") 
        display = "-" + display;
      else 
        display = display.substr(1); */
    }

    if (display.length > 16) {
      display = display.substr(0, 16);
//      stopEnter = true;
    }
  }

  if (buttonType(b) == "operation") {
    if (operation !== "") {
      //perform operation
      result = performOperation(operation, result, sign+display);
      if (result == "Error" || result == "Overflow")
        display = result;
    }
    else {
      result = sign+display;
//      sign = "";
    }
    operation = b;
    newNumber = true;
  }

  if (buttonType(b) == "equal") {
    if (operation !== "") {
      //perform operation
      result = performOperation(operation, result, sign+display);
      if (result[0] == "-") {
        sign = "-";
        display = result.substr(1);
      }
      else
        display = result;
      operation = "";
    }
    newNumber = true;
  }

  if (buttonType(b) == "reset") resetCalculator();

  disp.innerHTML = sign+display;
  console.log("\t\tdisplay = " + display);
  console.log("\t\tsign = " + sign);
  console.log("\t\tresult = >" + result+"< ("+result.length+")");
  console.log("\t\toperation = " + operation);
  console.log("\t\tnewNumber = " + newNumber);
  console.log("\t\tstopEnter = " + stopEnter);
}

function makeProcessFunc(b) {
  return function() {
    processButton(b);
  };
}

function pressKey(key) {
  window[key]["style"]["border-style"] = "inset";
  setTimeout(function() {
    window[key]["style"]["border-style"] = "outset";
    window[key]["style"]["border-style"] = "";
  }, 250);
}

function numpadHandler(e) {
  switch (e.key) {
    case "0":
    case "1":
    case "2":
    case "3":
    case "4":
    case "5":
    case "6":
    case "7":
    case "8":
    case "9":
      pressKey("b" + e.key);
      processButton(e.key);
      break;
    case ".":
      pressKey("bF"); //Fractional
      processButton(e.key);
      break;
    case "+":
      pressKey("bA"); //Addition
      processButton(e.key);
      break;
    case "-":
      pressKey("bS"); //Substraction
      processButton(e.key);
      break;
    case "*":
      pressKey("bM"); //Multiplication
      processButton(e.key);
      break;
    case "/":
      pressKey("bD"); //Division
      processButton(e.key);
      break;
    case "^":
      pressKey("bP"); //Power
      processButton(e.key);
      break;
    case "=":
    case "Enter":
      pressKey("bE"); //Equal
      processButton("=");
      break;
    case "c":
    case "C":
    case "Escape":
      pressKey("bC"); //Clear
      processButton("c");
      break;
  }
  console.log(e);
}

b1.onclick = makeProcessFunc("1");
b2.onclick = makeProcessFunc("2");
b3.onclick = makeProcessFunc("3");
b4.onclick = makeProcessFunc("4");
b5.onclick = makeProcessFunc("5");
b6.onclick = makeProcessFunc("6");
b7.onclick = makeProcessFunc("7");
b8.onclick = makeProcessFunc("8");
b9.onclick = makeProcessFunc("9");
b0.onclick = makeProcessFunc("0");
bN.onclick = makeProcessFunc("n");
bC.onclick = makeProcessFunc("c");
bD.onclick = makeProcessFunc("/");
bM.onclick = makeProcessFunc("*");
bS.onclick = makeProcessFunc("-");
bA.onclick = makeProcessFunc("+");
bF.onclick = makeProcessFunc(".");
bP.onclick = makeProcessFunc("^");
bE.onclick = makeProcessFunc("=");

resetCalculator();
addEventListener("keydown", numpadHandler);


              
            
!
999px

Console