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

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="container">
  <div class="row">
    <div class="calc-container col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3 col-xs-8 col-xs-offset-2">
      <div class="row well display-well">
        <p class="pull-right" id="display-text"></p>
      </div>
      <div class="row">
        <div class="btn-group btn-group-lg btn-group-justified" role="group" aria-label="...">
          <a class="btn btn-danger" data-calc="AC" id="allClear">AC</a>
          <a class="btn btn-warning" data-calc="%">%</a>
          <a class="btn btn-warning" data-calc="+/-">+/-</a>
          <a class="btn btn-warning" data-calc="/" id="divide">÷</a>
        </div>
      </div>
      <div class="row">
        <div class="btn-group btn-group-lg btn-group-justified" role="group" aria-label="...">
          <a class="btn btn-default middle-rows" data-calc="7">7</a>
          <a class="btn btn-default middle-rows" data-calc="8">8</a>
          <a class="btn btn-default middle-rows" data-calc="9">9</a>
          <a class="btn btn-warning middle-rows" data-calc="*">x</a>
        </div>
      </div>
      <div class="row">
        <div class="btn-group btn-group-lg btn-group-justified" role="group" aria-label="...">
          <a class="btn btn-default middle-rows" data-calc="4">4</a>
          <a class="btn btn-default middle-rows" data-calc="5">5</a>
          <a class="btn btn-default middle-rows" data-calc="6">6</a>
          <a class="btn btn-warning middle-rows" data-calc="-">-</a>
        </div>
      </div>
      <div class="row">
        <div class="btn-group btn-group-lg btn-group-justified" role="group" aria-label="...">
          <a class="btn btn-default middle-rows" data-calc="1">1</a>
          <a class="btn btn-default middle-rows" data-calc="2">2</a>
          <a class="btn btn-default middle-rows" data-calc="3">3</a>
          <a class="btn btn-warning middle-rows" data-calc="+">+</a>
        </div>
      </div>
      <div class="row">
        <div class="btn-group btn-group-lg btn-group-justified">
          <a class="btn btn-default" data-calc="0" id="zero">0</a>
          <a class="btn btn-default" data-calc=".">.</a>
          <a class="btn btn-info" data-calc="C">C</a>
          <a class="btn btn-primary" data-calc="=" id="equals">=</a>
        </div>
      </div>
    </div>
  </div>
</div>
<div id="push"></div>
<div class="container-fluid">
  <div class="text-center" id="footer">
    <p class="muted credit">&copy <a href="https://jonfulk.com" target="_blank">Jonathan Fulk</a> 2016. All Rights Reserved.</p>
  </div>
</div>
              
            
!

CSS

              
                body {
  height: 100%;
  background-image: url("https://s20.postimg.org/s0qx6i78t/Calculation_Formulae.jpg");
  background-attachment: fixed;
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
}

#display-text {
  font-size: 36px;
}

.display-well {
  -webkit-box-shadow: inset 0px 0px 14px 0px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: inset 0px 0px 14px 0px rgba(0, 0, 0, 0.75);
  box-shadow: inset 0px 0px 14px 0px rgba(0, 0, 0, 0.75);
  background-color: #e6d8bd;
  height: 80px;
}

.calc-container {
  border: 1px solid black;
  border-radius: 5%;
  box-shadow: 15px 15px 15px;
  margin-top: 50px;
  padding: 30px;
  background-color: #d3d3d3;
}

.middle-rows {
  border-radius: 0 !important;
}

#allClear {
  border-bottom-left-radius: 0;
}

#divide {
  border-bottom-right-radius: 0;
}

#equals {
  border-top-right-radius: 0;
}

#zero {
  border-top-left-radius: 0;
}


/* Set the fixed height of the footer here */

#push,
#footer {
  height: 50px;
  color: #000;
  color: white;
}

/* Lastly, apply responsive CSS fixes as necessary */

@media (max-width: 767px) {
  #footer {
    margin-left: -20px;
    margin-right: -20px;
    padding-left: 20px;
    padding-right: 20px;
  }
}
              
            
!

JS

              
                function Calculator(display) {
  this.$displayElement = display;
  this.displayMax = 13;
  this.display = '0';
  this.floats = {last: null, current: 0.0};
  this.operators = {last: null, current: null};
  this.reset = false;
}

Calculator.prototype = {
  do: function(btn) {
    // perform an operation based on what button was pressed
    switch(btn) {
      case '+':
      case '-':
      case '*':
      case '/':
      case '=':
        this.doMath(btn);
        break;
      case 'AC':
        this.allClear();
        break;
      case 'C':
        this.clear();
        break;
      case '+/-':
        this.reverseSign();
        break;
      case '%':
        this.makePercent();
        break;
      default:
        this.updateState(btn);
        break;
    }
  },
  updateString: function(input) {
    // Update stringVal
    if (this.display.length < this.displayMax) {
      // don't accept input that will cause displayMax
      // to be exceeded
      if (this.display === '0') {
        // handle all input when stringVal is zero
        if (input !== '0' && input !== '.') {
          // if we have a non-zero number, set stringVal
          this.display = input;
        } else if (input === '.') {
          // if we have a dot, append stringVal
          this.display += input;
        }
      } else if (this.display !== '0') {
        // handle all input when stringVal is non-zero
        if ((this.display.indexOf('.') === -1) || (input !== '.')) {
          // protect against adding a decimal point
          this.display += input;
        }
      }
    }
  },
  updateCurrentFloat: function() {
    // Simple parse stringVal to float
    this.floats.current = parseFloat(this.display);
  },
  updateState: function(input) {
    // Update calculator string/float vals, then update calc display
    if(this.reset) {
      this.setState(0);
      this.reset = false;
    }
    this.updateString(input);
    this.updateCurrentFloat();
    this.updateDisplay();
  },
  reverseSign: function() {
    var reverse = (this.floats.current * -1);
    //set current float to new sign
    this.setState(reverse);
    this.updateDisplay();
  },
  setState: function(num) {
    // manually set our state after operations
    this.floats.current = parseFloat(num);
    this.display = num.toString();
  },
  resetOperators: function() {
    this.operators = {last: null, current: null};
  },
  updateDisplay: function() {
    // Simple display raw stringVal
    this.$displayElement.html(this.display);
  },
  allClear: function() {
    // Reset calc to default vals
    this.display = '0';
    this.floats = {last: null, current: 0.0};
    this.resetOperators();
    this.updateDisplay();
  },
  clear: function() {
  	// Reset current
    this.display = '0';
    this.floats.current = 0.0;
    this.updateDisplay();
  },
  makePercent: function() {
    //make current float a percentage when last float is 0 or null
    if (this.floats.last === null || this.floats.last === 0) {
      this.floats.current = this.floats.current / 100;
      this.setState(this.floats.current);
      this.updateDisplay();
    }
    //make current float a percentage before addition/subtraction operation
    //make current float a percentage before multiplicaiton/division operation
  },
  updateOperators: function(operator) {
    // Manage state of current/last operations
    if (this.operators.current === null) {
    	this.operators.current = operator;
    } else {
    	this.operators.last = this.operators.current;
      this.operators.current = operator;
    }
  },
  prepareOperation: function() {
    // copy current value > last value
    // covers when we press an operator for the first time
    if (this.floats.last === null) {
      this.floats.last = this.floats.current;
    }
    // we have already prepped an operation once
    else {
      if (this.operators.last !== null) {
        // we are chaining operations
        this.doOperation(this.operators.last);
      }
      else {
        // we are performing a single operation (most likely with = btn)
        this.doOperation(this.operators.current);
      }
    }
  },
  doOperation: function(operator) {
    // update calc state with result of pending operation
    switch (operator) {
      case '+':
        this.floats.last += this.floats.current;
        break;
      case '-':
        this.floats.last -= this.floats.current;
        break;
      case '*':
        this.floats.last *= this.floats.current;
        break;
      case '/':
        this.floats.last /= this.floats.current;
        break;
    }
    // manually set state to operated value
    this.setState(this.floats.last);
  },
  doMath: function(operator) {
    // set operators.current
    this.updateOperators(operator);
    // copy floats/strings.current > floats/strings.last (maybe using operators.last if non-null)
    this.prepareOperation();
    // check if it's time to freeze state
    if (operator === '=') {
      this.resetOperators();
      this.floats.last = null;
    }
    // updateState with inputs to current strings/floats
    this.updateDisplay();
    this.reset = true;
  }
};

//create event handlers

$(document).ready(function() {
  // init calculator & updateDisplay with default value showing
  var calc = new Calculator($('#display-text'));
  calc.updateDisplay();

  //capture events for all buttons
  $('a.btn').on('click', function() {
    calc.do($(this).attr('data-calc'));
  });

});
              
            
!
999px

Console