Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm 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. We'll also process your JavaScript with Babel.

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

Code Indentation

     

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.

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="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>
            
          
!
            
              @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);
  }
}

            
          
!
            
              "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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console