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

Packages

Add Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import or require. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="main">
    <header class="title">
        <p>FCC</p>
        <p>My Electronic Calculator <span class="digits">15 digits</span> </p>
    </header>
    <div class="screen"></div>
    <div class="buttons">
        <span id="logo">Y-Studio</span>
        <button id="ac">AC</button>
        <button id="ce">CE</button>
        <button id="leftArrow">&rtrif;</button>

        <button class="operator" value="/">&divide;</button>
        <button class="number" value="7">7</button>
        <button class="number" value="8">8</button>
        <button class="number" value="9">9</button>

        <button class="operator" value="*">&times;</button>
        <button class="number" value="4">4</button>
        <button class="number" value="5">5</button>
        <button class="number" value="6">6</button>

        <button class="operator" value="-">&ndash;</button>
        <button class="number" value="1">1</button>
        <button class="number" value="2">2</button>
        <button class="number" value="3">3</button>

        <button class="operator" value="+">+</button>
        <button class="number" value="0">0</button>
        <button class="number" value=".">&bull;</button>
        <button id="equal">=</button>

        <button class="operator" value="^">^</button>
        <button class="bracket" value="(">(</button>
        <button class="bracket" value=")">)</button>


    </div>
</div>
            
          
!
            
              @font-face {
  font-family: "Remachine Script Personal Use";
  src: url("https://crossorigin.me/https://db.onlinewebfonts.com/t/28ac560e1821e50a2af0f8b0830c5998.woff") format("woff"), url("https://crossorigin.me/https://db.onlinewebfonts.com/t/28ac560e1821e50a2af0f8b0830c5998.svg#Remachine Script Personal Use") format("svg");
}

@font-face {
  font-family: 'digital-7';
  src: url('https://crossorigin.me/https://github.com/Y-Taras/FreeCodeCamp/raw/master/Calculator/fonts/digital-7%20(italic).ttf') format('truetype');
}

body {
  background-color: #3a73ba;
}

.main {
  max-width: 300px;
  background: #C5C4C2;
  margin: 0 auto;
  padding: 1%;
  position: relative;
  top: 100px;
  border-radius: 23px/30px;
  box-shadow: 8px 2px 24px 7px grey inset;
}

header > p {
  text-align: center;
  font-family: "Remachine Script Personal Use", Serif;
  font-size: 30px;
  color: #402449;
  margin: 0;
}

header > p:last-child {
  margin-left: 24%;
  margin-right: 6px;
  font-size: 22px;
  color: #424242;
}

header > p:last-child > span.digits {
  float: right;
  font-family: 'Raleway', sans-serif;
  font-size: 16px;
  line-height: 26px;
}

.screen {
  height: 44px;
  width: auto;
  font: 36px "digital-7", Serif;
  color: #555555;
  background: #929080;
  margin: 6px;
  padding: 0 5px;
  border: 1px solid #c6c6c6;
  box-shadow: 4px 1px 4px 4px grey inset;
  border-radius: 5px 5px;
  text-align: right;
  line-height: 40px;
}

div.buttons > div#logo {
  float: left;
  width: 59px;
  font-size: 28px;
  margin: 6px;
  padding: 4px;
}

div.buttons > button {
  width: 59px;
  margin: 6px;
  padding: 6px;
  background-color: #272729;
  color: #e0e2e1;
  font-size: 24px;
  border-radius: 11px/15px;
  border: 2px solid #272729;
  box-shadow: 7px 6px 29px -12px rgba(0, 0, 0, 0.55);
  outline: 0;
  cursor: pointer;
}

div.buttons > button:active {
  transform: translateY(4px);
}

div.buttons > button::-moz-focus-inner {
  border: 0;
}

div.buttons > button.operator,
div.buttons > button.bracket,
div.buttons > button#equal,
div.buttons > button#leftArrow {
  background-color: #848896;
  box-shadow: 2px 2px 2px 2px #676b77 inset;
}

div.buttons > span#logo {
  font-family: "Remachine Script Personal Use", Serif;
  color: #424242;
  width: 59px;
  font-size: 25px;
  margin: 0;
  padding: 2px;
}

div.buttons > button#equal {
  height: 104px;
  float: right;
  margin-right: 10px;
}

div.buttons > button#ac,
div.buttons > button#ce {
  background-color: #5D2331;
}
            
          
!
            
              document.addEventListener("DOMContentLoaded", function() {
  "use strict";
  var stack = [],
    tempValue = "", //value of input number-string
    buttId = "",
    buttClass = "",
    buttVal = "", //Value of input
    button = document.querySelectorAll("button");

  var precedence = {
    "^": 4,
    "*": 3,
    "/": 3,
    "+": 2,
    "-": 2
  };
  var associativity = {
    "^": "Right",
    "*": "Left",
    "/": "Left",
    "+": "Left",
    "-": "Left"
  };

  document.getElementsByClassName("screen")[0].innerHTML = "0";

  function peek(array) {
    return array[array.length - 1];
  }

  function splitString(str) {
    var arrayData = str.split(/([\+\-\*\/\(\)\^])/);
    return arrayData.filter(Boolean);
  }

  var postToInfix = function(strTokens) {
    var parentheses = 0;
    var out = [],
      stack = [];

    // if string starts with either of those
    // ('.', '+', '*', '/', '^', '(') it must be incorrect
    while (/[\.\+\*\/\^)]/.test(strTokens.charAt(0))) {
      strTokens = strTokens.slice(1);
    }
    //checking number of parentheses
    var matchesArray1 = strTokens.match(/\(/g);
    var matchesArray2 = strTokens.match(/\)/g);
    // parentheses is broken
    if (matchesArray1 && matchesArray2 &&
      matchesArray1.length !== matchesArray2.length) {
      return null;
    }
    // special case where negation happens at the beginning of input
    if (strTokens.charAt(0) === '-') {
      strTokens = '0' + strTokens;
    }
    // find places where negation occurs (i.e. '(-3)') and place zeros
    // before minuses to make it standard subtraction
    strTokens = strTokens.replace(/\(-/gi, "(0-");
    var tokens = splitString(strTokens);

    for (i = 0; i < tokens.length; i += 1) {
      //If the token is a number, then add it to the output queue.
      if (!isNaN(tokens[i])) {
        out += tokens[i] + " ";
      }
      /* If the token is an operator, o1, then:*/
      if (precedence.hasOwnProperty(tokens[i])) {
        while ((precedence.hasOwnProperty(peek(stack))) &&
          ((associativity[tokens[i]] === "Left" && precedence[tokens[i]] <= precedence[peek(stack)]) ||
            (associativity[tokens[i]] === "Right" && precedence[tokens[i]] < precedence[peek(stack)]))) {
          out += stack.pop() + " ";
        }
        stack.push(tokens[i]);
      }
      //If the token is a left parenthesis (i.e. "("), then push it onto the stack.
      if (tokens[i] === "(") {
        stack.push(tokens[i]);
      }
      //If the token is a right parenthesis (i.e. ")"):
      if (tokens[i] === ")") {
        while (peek(stack) !== "(") {
          out += stack.pop() + " ";
        }
        if (peek(stack) === "(") {
          stack.pop();
        } else {
          console.log(" there are mismatched parentheses");
        }
      }
      console.log('i =', i, 'stack = ', stack, 'out = ', out);
    }
    while (stack.length) {
      if (peek(stack) === "(" || peek(stack) === ")") {
        console.log(" there are mismatched parentheses");
      }
      out += stack.pop() + " ";
    }
    console.log('stack = ', stack, 'out = ', out);
    return out;
  };

  function evalPostfixExpr(str) {
    if (str === null) {
      return "Error";
    }
    var arrayTokens = str.split(" ");
    //remove last "" element of the array, and reverse it;
    arrayTokens.pop();
    arrayTokens = arrayTokens.reverse();
    var stack = [];
    while (arrayTokens.length > 0) {
      var token = arrayTokens.pop();
      if (!isNaN(token)) {
        stack.push(token);
      } else {
        // not enough values on stack
        if (stack.length < 2) {
          console.log("Error");
        } else {
          var secondArg = Number(stack.pop());
          var firstArg = Number(stack.pop());
          var d = 0;

          switch (token) {
            case '+':
              d = firstArg + secondArg;
              break;

            case '-':
              d = firstArg - secondArg;
              break;

            case '*':
              d = firstArg * secondArg;
              break;

            case '/':
              d = firstArg / secondArg;
              break;

            case '^':
              d = Math.pow(firstArg, secondArg);
              break;
          }
          stack.push(d);
        }
      }
    }
    if (stack.length === 1) {
      return function() {
        var finalEval = stack.pop();
        var lengthNum = String(finalEval).length;
        if (Math.abs(Number(finalEval)) >= 1e+15) {
          return "error";
        } else if (lengthNum >= 16) {
          finalEval = String(finalEval).slice(0, 14);
          return Number(finalEval);
        } else {
          return Number(finalEval);
        }
      }();
    }
  }

  // Displays symbols on the calc screen
  function showData(data) {
    if (data === "") {
      document.getElementsByClassName("screen")[0].innerHTML = "0";
    } else {
      document.getElementsByClassName("screen")[0].innerHTML = data;
    }
  }

  var improveInput = function calcScreen(data) {

    var arrayData = splitString(data);
    var arrLength = arrayData.length;
    //getting index of last elem in array, that is a number
    var i = 1;
    while (isNaN(+arrayData[arrLength - i])) {
      if (arrLength === i) {
        break;
      }
      i += 1;
    }
    if ((arrayData[arrLength - i - 2] === "(" && arrayData[arrLength - i - 1] === "-") ||
      ((arrLength - i) === 1 && arrayData[0] === "-")) {
      return "-" + arrayData[arrLength - i];
    }
    return arrayData[arrLength - i];
  };

  function ceFunc(str) {
    var arr = splitString(str);
    if (!isNaN(+peek(arr))) {
      arr.pop();
      arr = arr.join("");
    }
    return arr;
  }

  function leftArrowFunc(str) {
    if (!/[\+\*\/\^\-\(\)]/.test(str.charAt(str.length - 1))) {
      str = str.slice(0, -1);
    }
    return str;
  }

  for (var i = 0; i < button.length; i++) {

    button[i].addEventListener("click", function() {

      buttId = this.id;
      buttClass = this.className;
      buttVal = this.value;

      switch (buttId) {
        case "ac":
          tempValue = "";
          showData(tempValue);
          break;
        case "ce":
          tempValue = ceFunc(tempValue);
          showData(improveInput(tempValue));
          break;
        case "leftArrow":
          tempValue = leftArrowFunc(tempValue);
          showData(improveInput(tempValue));
          break;
        case "equal":
          tempValue = evalPostfixExpr(postToInfix(tempValue));
          showData(tempValue);
          break;
        default:
          tempValue += buttVal;
          //second dot in a number or more than 15 digits. Illegal.
          if (/\d+\.\d+\./.test(tempValue) ||
            /[\d\.]{16}/.test(tempValue)) {
            tempValue = tempValue.slice(0, -1);
          }
          // there are two operators after each other
          if (/[\+\-\/\*\^]{2}/.test(tempValue)) {
            tempValue = tempValue.slice(0, -2) + buttVal;
          }
          // check if pressing multiple "0"
          if (/[\+\-\/\*\^\(\)]0\d$/.test(tempValue)) {
            tempValue = tempValue.slice(0, -2) + buttVal;
          }
          if (/^0\d/.test(tempValue)) {
            tempValue = buttVal;
          }
          // if string starts with either of those
          // ('.', '+', '*', '/', '^', '(') it must be incorrect
          if (tempValue.length === 1 && /[\.\+\*\/\^\)]/.test(tempValue)) {
            tempValue = "";
          } else {
            showData(improveInput(tempValue));
          }
      }
    });
  }
});
            
          
!
999px

Console