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

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 id="app">
   <div data-reactroot="" class="calculator">
      <div class="header">
         <div class="header__screen">
            <div class="formula"></div>
            <div id="display">0</div>
         </div>
      </div>
      <div class="buttons">
        <button id="seven" class="button number" value="7">7</button>
        <button id="eight" class="button number" value="8">8</button>
        <button id="nine" class="button number" value="9">9</button>
        <button id="clear" class="button button--red">AC</button>
        <button id="four" class="button number" value="4">4</button>
        <button id="five" class="button number" value="5">5</button>
        <button id="six" class="button number" value="6">6</button>
        <button id="div" class="button op" value="/">/</button>
        <button id="one" class="button number" value="1">1</button>
        <button id="two" class="button number" value="2">2</button>
        <button id="three" class="button number" value="3">3</button>
        <button id="mul" class="button op" value="*">*</button>
        <button id="zero" class="button number" value="0">0</button>
        <button id="undo" class="button" value="UNDO">undo</button>
        <button id="add" class="button op" value="+">+</button>
        <button id="sub" class="button op" value="-">-</button>
        <button id="equals" class="button button--full res" value="=">=</button></div>
   </div>
</div>

              
            
!

CSS

              
                @font-face {
  font-family: "Digital";
  src: url("//db.onlinewebfonts.com/t/1b40984d7a10bcae004877dc48f4cc8f.eot");
  src: url("//db.onlinewebfonts.com/t/1b40984d7a10bcae004877dc48f4cc8f.eot?#iefix") format("embedded-opentype"), url("//db.onlinewebfonts.com/t/1b40984d7a10bcae004877dc48f4cc8f.woff2") format("woff2"), url("//db.onlinewebfonts.com/t/1b40984d7a10bcae004877dc48f4cc8f.woff") format("woff"), url("//db.onlinewebfonts.com/t/1b40984d7a10bcae004877dc48f4cc8f.ttf") format("truetype"), url("//db.onlinewebfonts.com/t/1b40984d7a10bcae004877dc48f4cc8f.svg#Digital") format("svg");
}
html {
  font-size: 16px;
  font-family: Arial, Verdana, sans-serif;
}
#app {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.calculator {
  border-radius: 1rem;
  overflow: hidden;
  box-shadow: 0.05rem 0.05rem 0.2rem 0 #54545b;
}
button {
  cursor : pointer
}
.header {
  font-family: "Digital";
  text-align: right;
  color: #54545b;
  background: #54545b;
  padding: 0.8rem;
}
.header .header__screen {
  padding: 0.4rem;
  width: 15.63rem;
  background: #eaecd6;
  box-shadow: inset 0.2rem 0.2rem #c9cbb6;
  border-radius: 0.6rem;
}
.header .formula {
  font-size: 1.3rem;
  height: 1.5rem;
}
.header #display {
  font-size: 3rem;
}
.buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(5, 1fr);
  background: #f2f2f2;
  padding: 0.6rem;
}
.buttons .button {
  font-family: inherit;
  font-size: 1rem;
  color: #fff;
  background: #54545b;
  border: 0;
  outline: 0;
  padding: 0.5rem 0;
  margin: 0.6rem;
  border-radius: 0.3rem;
  box-shadow: 0.05rem 0.05rem 0.2rem 0 #54545b;
}
.buttons .button.button--red {
  background: #eb5151;
}
.buttons .button.button--full {
  grid-column-start: 1;
  grid-column-end: 5;
}

              
            
!

JS

              
                function add(x, y) { return x + y; }
function sub(x, y) { return x - y; }
function mul(x, y) { return x * y; }
function div(x, y) { return x / y; }

//Command Class
class Command {
  constructor (execute, undo, value){
    this.execute = execute;
    this.undo = undo;
    this.value = value;
  }
}


class AddCommand extends Command {
  constructor (value) {
    super(add, sub, value)
  }
}
class SubCommand extends Command {
  constructor (value) {
    super(sub, add, value)
  }
}
class MulCommand extends Command {
  constructor (value) {
    super(mul, div, value)
  }
}

class DivCommand extends Command {
  constructor (value) {
    super(div, mul, value)
  }
}


//Reciever
class Calculator {
  constructor (){
    this.current = 0;
    this.history = [];
  }
  execute (command) {    
    this.current =  command.execute(this.current, command.value);
    this.history.push(command);
  }
  undo() {
    var command = this.history.pop();
    this.current = command.undo(this.current, command.value);
  }
  setCurrent (val){
    this.current = Number(val)
  }
  getValue (){ 
    return this.current
  }
  reset (){
    this.current = 0;
    this.history = [];
  }
}


















//RUN CALCULATOR WITH JQUERY
var calculator = new Calculator();
var opMode = false;
var setCommandType = type => val => {
   val = parseInt(val)
   switch (type) {
     case 'add' : return calculator.execute(new AddCommand(val));
     case 'sub' : return calculator.execute(new SubCommand(val));
     case 'div' : return calculator.execute(new DivCommand(val));
     case 'mul' : return calculator.execute(new MulCommand(val));
       
   }
};

$('.op').click(function () {
  CURRENT_COMMAND = setCommandType($(this).attr('id'));
  $('.formula').text($(this).val())

  opMode = true
})
$('.number').click(function () {
  if (!opMode) {
    $('#display').text( $(this).val())
    calculator.setCurrent($(this).val())
    return
  }
  $('.formula').text($('.formula').text()+' '+$(this).val())
  CURRENT_COMMAND($(this).val())
})

//UNDO
$('#undo').click(() => {
  calculator.undo()
  $('#display').text(calculator.getValue())
     $('.formula').text('')

})
//RESET
$('#clear').click(() => {
   calculator.reset();
   $('#display').text(calculator.getValue())
   $('.formula').text('')
    opMode = false

})

  
$('#equals').click(() => {
    $('#display').text(calculator.getValue())
    $('.formula').text(calculator.getValue())
})

              
            
!
999px

Console