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="display">0</div>
  <div class="upper-keypad">
    <button class="black fx normal" value="pow">x<sup>y</sup></button>
    <button class="black fx normal" value="log">LOG</button>
    <button class="black fx normal" value="ln">LN</button>
    <button class="black fx normal" value="exp">e<sup>x</sup></button>
    <button class="blue fx normal" value="clr">CLR</button>
    
    <button class="black fx normal" value="sqrt">√x</button>
    <button class="black fx normal" value="arc">ARC</button>
    <button class="black fx normal" value="sin">SIN</button>
    <button class="black fx normal" value="cos">COS</button>
    <button class="black fx normal" value="tan">TAN</button>
    
    <button class="black fx normal" value="inv">1/x</button>
    <button class="black fx normal" value="swap">x⇄y</button>
    <button class="black fx normal" value="roll">R⬇</button>
    <button class="black fx normal" value="sto">STO</button>
    <button class="black fx normal" value="rcl">RCL</button>
    
    <button class="blue fx wide" value="enter">ENTER ⬆</button>
    <button class="black fx normal" value="chs">CHS</button>
    <button class="black fx normal" value="eex">EEX</button>
    <button class="black fx normal" value="clx">CLx</button>
  </div>
  <div class="lower-keypad">
    <button class="blue fx normal" value="-">&minus;</button>
    <button class="white num normal" value="7">7</button>
    <button class="white num normal" value="8">8</button>
    <button class="white num normal" value="9">9</button>
    
    <button class="blue fx normal" value="+">+</button>
    <button class="white num normal" value="4">4</button>
    <button class="white num normal" value="5">5</button>
    <button class="white num normal" value="6">6</button>
    
    <button class="blue fx normal" value="*">×</button>
    <button class="white num normal" value="1">1</button>
    <button class="white num normal" value="2">2</button>
    <button class="white num normal" value="3">3</button>
    
    <button class="blue fx normal" value="/">÷</button>
    <button class="white num normal" value="0">0</button>
    <button class="white num normal" value=".">.</button>
    <button class="white num normal" value="pi">π</button>
    
  </div>
  <div class="stack-preview">
    <div>T: <span id="T"></span></div>
    <div>Z: <span id="Z"></span></div>
    <div>Y: <span id="Y"></span></div>
    <div>X: <span id="X"></span></div>
    <div>S: <span id="S"></span></div>
    <div>xdone: <span id="xdone"></span></div>
    <div>enter: <span id="enter"></span></div>
    <div>minus: <span id="minus"></span></div>
  </div>
</div>
<span>
  <img id="backlabel" src="http://www.keesvandersanden.nl/calculators/images/HP35_1302S48386_backlabel.jpg">
</span>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Inconsolata|Source+Sans+Pro&subset=greek,latin-ext');

.calculator {
  border: 2px solid black;
  width: 300px;
  margin: 20px auto;
  padding: 10px;
  background-color: #4C474B;
  
  .display {
    border: 2px solid black;
    background-color: #4D0512;
    color: red;
    margin: 10px 5px;
    margin-bottom: 20px;
    padding: 10px 20px;
    font-size: 24px;
    font-family: Inconsolata, Consolas, monospace;
    text-overflow: clip;
    // text-align: right;
    
  }
  
  .upper-keypad {
    display: grid;
    grid-template: repeat(4, 1fr) / repeat(5, 1fr);
    
  }
  
  .lower-keypad {
    display: grid;
    grid-template: repeat(4, 1fr) / repeat(4, 1fr);
    
    .num {color: black}
  }
  
  .upper-keypad, .lower-keypad {
    button {
      display: flex;
      padding: 10px;
      margin: 5px;
      color: white;
      justify-content: center;
      border: 0;
      border-radius: 4px;
      font-family: Source Sans Pro, sans-serif;
      font-size: 18px;
      cursor: pointer;
    }
  }
  
  $black: #222222;
  $blue: #5EB6C4;
  $white: #FDFEEE;
  
  .black {background-color: $black;
    &:hover {
      background-color: darken($black, 10%);
    } 
  }
  .blue {
    background-color: $blue;
    &:hover {
      background-color: darken($blue, 10%);
    }
  }
  .white {
    background-color: $white;
    &:hover {
      background-color: darken($white, 10%);
    } 
  }
  
  .enabled {
    border: 1px solid white;
    background-color: lighten($black, 20%);
    &:hover {
      background-color: lighten($black, 40%);
    } 
  }
  
  .normal {grid-area: auto / span 1;}
  .wide {grid-area: auto / span 2;}
  
  .stack-preview {
    font-family: Inconsolata, Consolas, monospace;
    color: white;
    display: none;    // comment out this line for debugging
  }
}

#backlabel {
  margin: auto;
  display: block;
}
              
            
!

JS

              
                $('.calculator').each(function() {
  let calculator = $(this);
  let display = $('.display');
  let stack = {
    'x': 0,  // display register
    'y': 0,
    'z': 0,
    't': 0,
    's': 0  // storage register
  };
  let arc = false;
  let xdone = true;   // if false, entry for x is not yet done
  let enter = false;  // if true, does not immediately lift stack
  let minus = true;   // if true, allow direct entry of (-) sign
  let error = false;
  let manual_update = false;  // if true, update() is overriden
  
  let funcs = {
    'pow': (x,y) => Math.pow(x,y),
    'log': (x) => Math.log10(x),
    'ln': (x) => Math.log(x),
    'exp': (x) => Math.exp(x),
    'sqrt': (x) => Math.sqrt(x),
    'sin': (x) => (arc ? Math.asin(x)*180/Math.PI : Math.sin(x*Math.PI/180)),
    'cos': (x) => (arc ? Math.acos(x)*180/Math.PI : Math.cos(x*Math.PI/180)),
    'tan': (x) => (arc ? Math.atan(x)*180/Math.PI : Math.tan(x*Math.PI/180)),
    'inv': (x) => 1/x,
    '+': (x,y) => (y|0)+(x|0),
    '-': (x,y) => y-x,
    '*': (x,y) => y*x,
    '/': (x,y) => y/x
  };
  
  let update = function() {
    if (stack.x === '-') {
      display.html('-0');
    }
    else if (isNaN(stack.x) || !isFinite(stack.x)) {
      display.html('error')
    }
    else display.html(stack.x);
    
    if (arc) {
      $('button[value="arc"]').addClass('enabled');
    } else {
      $('button[value="arc"]').removeClass('enabled');
    }
    
    $('#T').html(stack.t);
    $('#Z').html(stack.z);
    $('#Y').html(stack.y);
    $('#X').html(stack.x);
    $('#S').html(stack.s);
    $('#xdone').html(xdone);
    $('#enter').html(enter);
    $('#minus').html(minus);
  }

  let keys = $('.upper-keypad, .lower-keypad', calculator);
  $('.fx', keys).click(function() {
    let fn = $(this).val();
    switch (fn) {
      case 'enter':
        stack.t = stack.z;
        stack.z = stack.y;
        stack.y = stack.x;
        enter = true;
        break;
      case 'swap':
        let x = stack.x, y = stack.y;
        stack.x = y;
        stack.y = x;
        break;
      case 'roll':
        let oldt = stack.t;
        stack.t = stack.x;
        stack.x = stack.y;
        stack.y = stack.z;
        stack.z = oldt;
        break;
      case 'clr':
        stack.t = 0;
        stack.x = 0;
        stack.y = 0;
        stack.z = 0;
        stack.s = 0;
        minus = true;
        break;
      case 'chs':
        if (minus) {
          if (xdone) {
            stack.x = '-';
            xdone = false;
            // console.log('   xdone: ' + xdone);
          } else stack.x += '-';
          minus = false;
        } else {
          stack.x = -parseFloat(stack.x);
        }
        break;
      case 'eex':
        minus = true;
        xdone = false;
        if (stack.x === 0) stack.x = 1;
        stack.x += 'e';
        break;
      case 'clx':
        stack.x = 0;
        minus = true;
        xdone = true;
        break;
      case 'sto':
        stack.s = stack.x;
        break;
      case 'rcl':
        if (!enter) {
          stack.t = stack.z;
          stack.z = stack.y;
          stack.y = stack.x;
        }
        stack.x = stack.s;
        break;
      case 'arc':
        arc = true;
        break;
      default:
        let twoargs = ['pow', '+', '-', '*', '/'];
        let trig = ['sin', 'cos', 'tan'];
        if (twoargs.indexOf(fn) !== -1) { // if fn needs 2 args
          stack.x = funcs[fn](stack.x, stack.y);
          stack.y = stack.z;
          stack.z = stack.t;
        } else {
          stack.x = funcs[fn](stack.x);
          // following any trig function
          // z is duplicated into register T
          if (trig.indexOf(fn) !== -1) {
            stack.t = stack.z;
          }
        }
        if (arc) arc = false;
        break;
    }
    if (['chs','eex','clx'].indexOf(fn) === -1) xdone = true;
    update();
  });

  $('.num', keys).click(function() {
    let n = $(this).val();
    if (n === 'pi') {
      stack.t = stack.z;
      stack.z = stack.y;
      stack.y = stack.x;
      stack.x = Math.PI;
      enter = false;
    } else if (n === '.') {
      stack.x += n;
    } else {
      if (xdone) {
        if (!enter) {
          stack.t = stack.z;
          stack.z = stack.y;
          stack.y = stack.x;
        }
        stack.x = parseFloat(n);
        xdone = false;
        enter = false;
      } else {
        stack.x += '' + n;
      }
    }
    minus = false;
    update();
  });
});

              
            
!
999px

Console