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 class="calculator">
  <div class="input " id="input"></div>
  <!-- the buttons -->
  <div class="calculator-buttons">
    <button class="calc-button clear" id="clear">C</button>
    <button class="calc-button operators">÷</button>
    <button class="calc-button numbers">7</button>
    <button class="calc-button numbers">8</button>
    <button class="calc-button numbers">9</button>
    <button class="calc-button operators">×</button>
    <button class="calc-button numbers">4</button>
    <button class="calc-button numbers">5</button>
    <button class="calc-button numbers">6</button>
    <button class="calc-button operators">−</button>
    <button class="calc-button numbers">1</button>
    <button class="calc-button numbers">2</button>
    <button class="calc-button numbers">3</button>
    <button class="calc-button operators">&plus;</button>

    <button class="calc-button numbers zero">0</button>
    <button class="calc-button equal" id="result">&equals;</button>
  </div>

</div>
              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css?family=Space+Mono");

/* quick reset so all our padding is the right size */
* {
  box-sizing: border-box;
}
/* add some spacing */
body {
  background: linear-gradient(to bottom, #ffa17f, #00223e);
  padding-bottom: 30px;
  padding-top: 30px;
  font-family: "Space Mono";
}
/* limit the width and center */
.calculator {
  background-color: white;
  max-width: 400px;
  margin: 0 auto;
  /*   border: 1px solid #111; */
  border-radius: 5px;
  box-shadow: 6px 9px 5px 0px rgba(0, 0, 0, 0.26);
}
.calculator-buttons {
  padding: 20px;
  display: grid;
  grid-gap: 15px;
  grid-template-columns: repeat(4, 1fr);
}
.calc-button {
  background: white;
  border: 1px solid #111; /* black border */
  padding: 20px;
  color: black; /* white text */
  border-radius: 5px; /* rounded corners */
  font-size: 22px; /* larger fonts */
  cursor: pointer; /* make it look clickable */
  transition: all .2s ease-in-out;
  &:hover {
    background: rgba(0, 0, 0, 0.1);
    box-shadow: inset 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
  }
}

.input {
  height: 70px;
  padding: 10px;
  width: 100%;
  border-bottom: 1px solid #111;
  color: #333;
  text-align: right;
  font-size: 40px;
}
.zero,
.clear {
  grid-column: span 3;
}

.clear {
  background: orange;
  &:hover {
    background: darken(orange, 10%);
    box-shadow: inset 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
  }
}
.equal {
  background: black;
  color: white;
  &:hover {
    background: white;
    color: black;
    box-shadow: inset 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
  }
}

              
            
!

JS

              
                "use strict";

var input = document.getElementById("input"), // input/output button
  number = document.querySelectorAll(".numbers"), // number buttons
  operator = document.querySelectorAll(".operators"), // operator buttons
  result = document.getElementById("result"), // equal button
  clear = document.getElementById("clear"), // clear button
  resultDisplayed = false; // flag to keep an eye on what output is displayed

// adding click handlers to number buttons
for (var i = 0; i < number.length; i++) {
  number[i].addEventListener("click", function(e) {
    // storing current input string and its last character in variables - used later
    var currentString = input.innerHTML;
    var lastChar = currentString[currentString.length - 1];

    // if result is not diplayed, just keep adding
    if (resultDisplayed === false) {
      input.innerHTML += e.target.innerHTML;
    } else if (
      (resultDisplayed === true && lastChar === "+") ||
      lastChar === "-" ||
      lastChar === "×" ||
      lastChar === "÷"
    ) {
      // if result is currently displayed and user pressed an operator
      // we need to keep on adding to the string for next operation
      resultDisplayed = false;
      input.innerHTML += e.target.innerHTML;
    } else {
      // if result is currently displayed and user pressed a number
      // we need clear the input string and add the new input to start the new opration
      resultDisplayed = false;
      input.innerHTML = "";
      input.innerHTML += e.target.innerHTML;
    }
  });
}

// adding click handlers to number buttons
for (var i = 0; i < operator.length; i++) {
  operator[i].addEventListener("click", function(e) {
    // storing current input string and its last character in variables - used later
    var currentString = input.innerHTML;
    var lastChar = currentString[currentString.length - 1];

    // if last character entered is an operator, replace it with the currently pressed one
    if (
      lastChar === "+" ||
      lastChar === "-" ||
      lastChar === "×" ||
      lastChar === "÷"
    ) {
      var newString =
        currentString.substring(0, currentString.length - 1) +
        e.target.innerHTML;
      input.innerHTML = newString;
    } else if (currentString.length == 0) {
      // if first key pressed is an opearator, don't do anything
      console.log("enter a number first");
    } else {
      // else just add the operator pressed to the input
      input.innerHTML += e.target.innerHTML;
    }
  });
}

// on click of 'equal' button
result.addEventListener("click", function() {
  // this is the string that we will be processing eg. -10+26+33-56*34/23
  var inputString = input.innerHTML;

  // forming an array of numbers. eg for above string it will be: numbers = ["10", "26", "33", "56", "34", "23"]
  var numbers = inputString.split(/\+|\-|\×|\÷/g);

  // forming an array of operators. for above string it will be: operators = ["+", "+", "-", "*", "/"]
  // first we replace all the numbers and dot with empty string and then split
  var operators = inputString.replace(/[0-9]|\./g, "").split("");

  console.log(inputString);
  console.log(operators);
  console.log(numbers);
  console.log("----------------------------");

  // now we are looping through the array and doing one operation at a time.
  // first divide, then multiply, then subtraction and then addition
  // as we move we are alterning the original numbers and operators array
  // the final element remaining in the array will be the output

  var divide = operators.indexOf("÷");
  while (divide != -1) {
    numbers.splice(divide, 2, numbers[divide] / numbers[divide + 1]);
    operators.splice(divide, 1);
    divide = operators.indexOf("÷");
  }

  var multiply = operators.indexOf("×");
  while (multiply != -1) {
    numbers.splice(multiply, 2, numbers[multiply] * numbers[multiply + 1]);
    operators.splice(multiply, 1);
    multiply = operators.indexOf("×");
  }

  var subtract = operators.indexOf("-");
  while (subtract != -1) {
    numbers.splice(subtract, 2, numbers[subtract] - numbers[subtract + 1]);
    operators.splice(subtract, 1);
    subtract = operators.indexOf("-");
  }

  var add = operators.indexOf("+");
  while (add != -1) {
    // using parseFloat is necessary, otherwise it will result in string concatenation :)
    numbers.splice(
      add,
      2,
      parseFloat(numbers[add]) + parseFloat(numbers[add + 1])
    );
    operators.splice(add, 1);
    add = operators.indexOf("+");
  }

  input.innerHTML = numbers[0]; // displaying the output

  resultDisplayed = true; // turning flag if result is displayed
});

// clearing the input on press of clear
clear.addEventListener("click", function() {
  input.innerHTML = "";
});

              
            
!
999px

Console