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 id="calculator" class="calculator">
  <div class="result"><p></p></div>
  
  <span class="clear grey">AC</span>
  <span class="grey">+/-</span>
  <span class="grey">%</span>
  <span class="operator">÷</span>

  <span>7</span>
  <span>8</span>
  <span>9</span>
  <span class="operator">x</span>
  
  <span>4</span>
  <span>5</span>
  <span>6</span>
  <span class="operator">-</span>
  
  <span>1</span>
  <span>2</span>
  <span>3</span>
  <span class="operator">+</span>
  
  <span class="double">0</span>
  <span>.</span>
  <span class="operator">=</span>
  
</div>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Roboto:100);
html {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
*, *:before, *:after {
  -webkit-box-sizing: inherit;
  -moz-box-sizing: inherit;
  box-sizing: inherit;
}
html {
  font-family: 'Roboto';
  font-weight: 100;
}
body {
  display: flex;
  justify-content: center;
  margin-top: 1em;
  font-size: 2em;
  background-color: #bbb;
}
.calculator {
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  width: 300px;
  box-shadow: 0px 3px 10px 3px #aaa;
  border-right: 0.01em solid #333;
  border-bottom: 0.01em solid #333;
}
.result {
  width: 100%;
  background-color: #000;
  min-height: 2em;
  color: white;
}
.result > p {
  padding: 0 1em;
  text-align: right;
  line-height: 2em;
}
span {
  box-sizing: border-box;
  text-align: center;
  cursor: pointer;
  width: 25%;
  background-color: #ccc;
  line-height: 2em;
  border-top: 0.01em solid #333;
  border-left: 0.01em solid #333;
}
.operator {
  background-color: #FF9500;
  color: white;
}
.double {
  width: 50%;
}
.grey {
  background-color: #aaa;
}
.clicked {
  background-color: darken(orange, 10%);
}
              
            
!

JS

              
                window.onload = function() {
  var buttons = document.getElementsByTagName('span'), // Select all buttons
    result = document.querySelectorAll('.result p')[0], // Select the result-field
    clear = document.getElementsByClassName('clear')[0], // Select the clearAll-button
    equation = [], // create the equation array
    operator = false; // helper variable, tracks if operator was last button pushed

  for (var i = 0; i < buttons.length; i += 1) { // Add onclick events to buttons
    if (buttons[i].innerHTML === '=') {
      buttons[i].addEventListener("click", calculate(i));
    } else if (buttons[i].innerHTML === '+/-') {
      buttons[i].addEventListener("click", invert(i));
    } else if (buttons[i].innerHTML === '%') {
      buttons[i].addEventListener("click", percent(i));
    } else if (buttons[i].innerHTML === 'AC') {
      equation = [];
    } else {
      buttons[i].addEventListener("click", addValue(i));
    }
  }
  /*
   * Clear the result screen and equation string and reset operator
   */
  clear.onclick = function() {
      result.innerHTML = '';
      equation = [];
      operator = false;
    }
    /*
     * Add a value, number or operator, to the equation string. If an operator than first removes all other clicked classes and adds it to the button just pressed. Than checks if an operator was just pressed, in which case it replaces it with the button pressed. If type is a Number, than value was just calculated so empty the array and start a new equation. Otherwise you would add numbers to the end of your calculated value.
     */
  function addValue(i) {
    return function() {
      if (buttons[i].innerHTML === '÷') {
        clicked(this);
        ifOperatorThanSwap('/');
      } else if (buttons[i].innerHTML === 'x') {
        clicked(this);
        ifOperatorThanSwap('*');
      } else if (buttons[i].innerHTML === '+') {
        clicked(this);
        ifOperatorThanSwap('+');
      } else if (buttons[i].innerHTML === '-') {
        clicked(this);
        ifOperatorThanSwap('-');
      } else {
        removeClicked();
        if (checkIfNum(equation[equation.length - 1])) {
          equation = [];
          equation.push(buttons[i].innerHTML);
          operator = true;
        } else {
          equation.push(buttons[i].innerHTML);
        }
        if (operator) {
          result.innerHTML = buttons[i].innerHTML;
        } else {
          result.innerHTML += buttons[i].innerHTML;
        }
        operator = false;
      }
    };
  }
  /*
   * First removes any button with clicked class, then adds .clicked to pressed button
   */
  function clicked(i) {
    removeClicked(i);
    i.classList.add('clicked');
  }
  /*
   * Finds any elements with the clicked class and removes the class.
   */
  function removeClicked(i) {
    var elems = document.querySelectorAll(".clicked");
    [].forEach.call(elems, function(el) {
      el.classList.remove("clicked");
    });
  }
  /*
   * Calculate and print the equation array. Firsts joins all the elements in the equations array than evals the string of numbers and operators. Than it empties the equation array and puts the result as the first elemnt in the array. 
   */
  function calculate(i) {
    return function() {
      if (equation.length == 0) { // If nothing in array, do nothing
        return;
      } else {
        var answer = eval(equation.join(''));
        if (answer % 1 === 0) { // check if interger or float
          result.innerHTML = answer;
        } else { // if float than round to four numbers after decimal
          result.innerHTML = answer.toFixed(4);
        }
        equation = [];
        equation.push(answer);
        operator = false;
      }
    };
  }
  /*
   * Invert the current item in the viewer to and from a negative number.
   */
  function invert(i) {
    return function() {
      if (equation.length == 0) {
        return;
      } else {
        var number = result.innerHTML; // Grab number currently typed in
        popNumberOfDigits(number); // remove number of digits from equation array
        var invert = number * -1; // create inverted number by multiplying by -1
        equation.push(invert); // push to equation
        result.innerHTML = invert; // push to results display
      }
    }
  }
  /*
   * Changes the current number entered into a percentage.
   */
  function percent(i) {
    return function() {
      var number = result.innerHTML; // Grab number currently typed in
      popNumberOfDigits(number); // remove number of digits from equation array
      var percent = number * 0.01; // create percentage
      equation.push(percent); // push to equation
      result.innerHTML = percent.toFixed(2); // show in results display
    }
  }
  /*
   * Checks if the operator value to see if the last button pressed was an operator. If it is than it removed the operator and replaces it with the operator that was just pressed. ie If you first hit plus, but change your mind and hit minus, it removes the plus from the equation array and replaces it with minus. 
   */
  function ifOperatorThanSwap(str) {
    if (!operator) {
      equation.push(str);
      operator = true;
    } else {
      equation.pop();
      equation.push(str);
    }
  }
  /*
   * Checks if a number is of type string or number.
   */
  function checkIfNum(v) {
    if (typeof v == 'string') {
      return false;
    } else if (typeof v == 'number') {
      return true;
    }
  }
  /*
   * Checks the number of digits in the screen and removes them from the equation array. 
   */
  function popNumberOfDigits(number) {
    var arr = number.split(''); // Create an array holding each part of the number (eg. 13 = ['1', '3'] )
    for (i = 0; i < arr.length; i++) { // Removes the last few elements from equation array
      equation.pop();
    }
  }
};
              
            
!
999px

Console