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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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="calc ">
    <h4 class='title'>CALCULATOR</h4>
    <input class='display' id='result' disabled>
    <table class='keypad'>
     <tbody>
       <tr class="row">
         <td class="cell red">AC</td>
         <td class="cell red">CE</td>
         <td class="cell disabled" title='exponent'>^</td>
         <td class="cell">&divide;</td>
       </tr>
       <tr class="row">
         <td class="cell">7</td>
         <td class="cell">8</td>
         <td class="cell">9</td>
         <td class="cell">&times;</td>
       </tr>
       <tr class="row">
         <td class="cell">4</td>
         <td class="cell">5</td>
         <td class="cell">6</td>
         <td class="cell">-</td>
       </tr>
       <tr class="row">
         <td class="cell">1</td>
         <td class="cell">2</td>
         <td class="cell">3</td>
         <td class="cell" rowspan=2>+</td>
       </tr>
       <tr class="row">
         <td class="cell">0</td>
         <td class="cell">.</td>
         <td class="cell">=</td>
       </tr>
     </tbody>
    </table>

  </div>

 
              
            
!

CSS

              
                /* https://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}


/* HTML5 display-role reset for older browsers */

article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
  display: block;
}

body {
  line-height: 1;
}

ol,
ul {
  list-style: none;
}

blockquote,
q {
  quotes: none;
}

blockquote:before,
blockquote:after,
q:before,
q:after {
  content: '';
  content: none;
}

table {
  border-collapse: collapse;
  border-spacing: 0;
}


/*reset ends*/

body {
  font-family: arial;
  text-align: center;
  min-height: 100vh;
/*   background-color: #FFFEEF; */
  background-image: linear-gradient(#EEE 0,#FFFEEF 100vh)
}

.calc {
  display: inline-block;
  background: #FFFEFB;
  margin-top: 50px;
  padding: 0px 10px 20px;
  border-radius: 50%/15px;
  box-shadow: 0 12px 15px #B7B7B7, 0 -3px 8px rgba(80, 80, 80, 0.5)inset;
}

.title {
  margin-top: 30px;
  color: #bbb;
  letter-spacing: 3px;
}

.keypad {
  border-collapse: separate;
  border-spacing: 20px;
}

.display {
  text-align: right;
  display: inline-block;
  width: 196px;
  margin: 15px 20px;
  background: #E4FFEF;
  font-size: 2em;
  padding: 7px 10px;
  border-radius: 10px;
  box-shadow: 0 1px 9px #888 inset, 0px 2px 3px #aaa inset;
  color: #999;
  font-family: 'Orbitron', arial, sans-serif;
  border: none;
}

.cell {
  width: 40px;
  height: 30px;
  background: #413C4A;
  color: white;
  vertical-align: middle;
  border-radius: 50%/04px;
  cursor: pointer;
  text-align: center;
  font-weight: bold;
  box-shadow: 0 2px 0 #231919, 0 1px 5px rgba(35, 25, 25, 0.6), 0 1px 5px rgba(0, 0, 0, 0.29) inset, 0 3px 7px rgba(255, 255, 255, 0.32) inset;
}

.red {
  background-color: #B72A2A;
  box-shadow: 0 2px 0 #541111, 0 1px 5px rgba(35, 25, 25, 0.6);
}

.pressed {
  box-shadow: none;
  transform: scale(0.98);
  -ms-transform: scale(0.98);
}
              
            
!

JS

              
                function calc() {

  var a = undefined,
    op = undefined,
    b = undefined,
    stage = 0,
    aStr = '',
    bStr = '';
  var divideSign = '\u00f7';
  var multiplySign = String.fromCharCode(215);

  function solve(a, op, b) {
    switch (op) {
      case '+':
        return a + b;
      case '-':
        return a - b;
      case multiplySign:
        return a * b;
      case divideSign:
        return a / b;
      case '^':
        return Math.pow(a, b);
    }
  }

  function compute(c) {
    switch (stage) {
      case 0:
        if ((c >= 0 && c <= 9) || c == '.') {
          // a number or a dot
          aStr += c;
          $('#result').val(aStr);
        } else if (c == 'AC' || c == 'CE') {
          // clears everything stage becomes zero
          // but its already zero so no need to change
          aStr = '';
          $('#result').val(aStr);
        } else if (c == '=') {
          // do nothing in this case
          // it seems brings stage to zero
          stage = 0;
          aStr = '';
        } else {
          // some operator is pressed
          a = parseFloat(aStr);
          op = c;
          stage = 1;
          // console.log(a)
          // from the functionality of calc in stage 1 another operator overrides previous operator .. check that out
        }

        break;

      case 1:
        if ((c >= 0 && c <= 9) || c == '.') {
          // a number or a dot
          stage = 2;
          bStr += c;
          $('#result').val(bStr);
        } else if (c == 'CE') {
          // if(bStr==''){ // bStr will be empty if we are in stage 1
          stage = 0;
          aStr = '';
          op = undefined;
          /*}else{
            bStr='';
            stage='1'; // but its already 1 :P // this case should be in stage 2 maybe 
          }*/
          $('#result').val('');
        } else if (c == 'AC') {
          // clears everything stage becomes zero
          stage = 0;
          aStr = '';
          op = undefined;
          $('#result').val('');
        } else if (c == '=') {
          // do nothing in this case

        } else {
          // some operator is pressed
          op = c;
          // console.log(a)
          // from the functionality of calc in stage 1 another operator overrides previous operator .. check that out
        }
        break;

      case 2:
        if ((c >= 0 && c <= 9) || c == '.') {
          // a number or a dot

          bStr += c;
          $('#result').val(bStr);
        } else if (c == 'CE') {
          bStr = '';
          stage = 1;

        } else if (c == 'AC') {
          // clears everything stage becomes zero
          stage = 0;
          aStr = '';
          op = undefined;
          bStr = '';
          $('#result').val('');

        } else if (c == '=') {
          b = parseFloat(bStr);
          $('#result').val(solve(a, op, b));
          aStr = '';
          bStr = '';
          stage = 0;

        } else {
          // some operator is pressed
          b = parseFloat(bStr);
          console.log(a + ' ' + op + " " + b)
          bStr = '';
          a = solve(a, op, b);
          $('#result').val(a);
          op = c;
          stage = 1;

          // from the functionality of calc in stage 1 another operator overrides previous operator .. check that out
        }
        break;

    }

    // console.log("* * "+ aStr + " "+a+" " + bStr+' ' + b+' '+ op +  ' ' + stage );
  }

  return {
    compute: compute,
  }

}

var app = calc();

$(function() {

  $('.cell').on('mousedown', function() {
    $(this).toggleClass('pressed')
  });
  $('.cell').on('mouseup', function() {
    $(this).toggleClass('pressed');
    app.compute($(this).text());
  });
  $('.cell').on('mouseout', function() {
    $(this).removeClass('pressed')
  });

})
              
            
!
999px

Console