123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="calculator">

    <div id="top" class="flex">
      <span class="clear">C</span>
      <div id="screen">
        <span id="out" class="screen">0</span>
      </div>
    </div>
    <div id="keys" class="flex keys">
      <!-- operators and other keys -->
      <div class="row">
        <span class="num">7</span>
        <span class="num">8</span>
        <span class="num">9</span>
        <span class="operator">+</span>
      </div>
      <div class="row">
        <span class="num">4</span>
        <span class="num">5</span>
        <span class="num">6</span>
        <span class="operator">-</span>
      </div>
      <div class="row">
        <span class="num">1</span>
        <span class="num">2</span>
        <span class="num">3</span>
        <span class="operator">X</span>
      </div>
      <div class="row">
        <span class="num">0</span>
        <span class="dec">.</span>
        <span class="eval">=</span>
        <span class="operator">/</span>
      </div>
    </div>

</div>
<div id="hidden">
</div>
            
          
!
            
                /* Definitions */
  
  $screen: #C3E4FF;
  $number-button: #0071CE;
  $operation-button: darken($screen, 20%);
  $clear-button: #AF2400;
  $eval-button: #00AF77;
  $decimal-button: #6E6E6E;
  /* Mixins, functions and other helpers */
  
  @mixin button-size($width, $height) {
    line-height: $height;
    width: $width;
    margin: $height/18;
  }
  /* Reset CSS */
  
  * {
    margin: 0;
    padding: 0;
    font: 6vmin Arial, sans-serif;
    color: #FFFFFF;
    box-sizing: border-box;
  }
  
  body {
    background: #1C1C1C
  }
  /* Container element */
  
  // Use flexbox and percentages for a scaling view 
  #calculator {
    width: 80vh;
    height: 100vh;
    margin: auto;
    //border: 2px solid blue;
    display: flex;
    /* establish flex container */
    flex-direction: column;
    /* stack flex items vertically */
    justify-content: center;
  }
  
  .flex {
    min-width: 100px;
    display: flex;
    flex-direction: column;
  }
  
  #keys {
    //border: 1px solid green;
  }
  
  .flex .row {
    display: flex;
    flex-direction: row;
    span {
      display: block;
      min-width: 25px;
      min-height: 25px;
      //border: 1px dotted #000;
      text-align: center;
      padding: auto;
      font-size: 2em;
      @include button-size(18vmin, 18vmin);
      border-radius: 2vmin;
      &.num {
        background: $number-button;
        &:hover {
          background: $operation-button;
          color: darken($number-button, 20%)
        }
      }
      &.operator {
        background: $operation-button;
        &:hover {
          background: $number-button;
          color: lighten($operation-button, 20%)
        }
      }
      &.dec {
        background: $decimal-button;
        &:hover {
          background: darken($decimal-button, 20%);
          //color: darken($number-button, 20%)
        }
      }
      &.eval {
        background: $eval-button;
        &:hover {
          background: darken($eval-button, 10%);
          //color: darken($number-button, 20%)
        }
      }
      &:hover {
        cursor: pointer;
      }
    }
  }
  
  #top {
    //border: 1px solid red;
    display: flex;
    flex-direction: row;
  }
  
  .clear {
    display: inline-block;
    background: $clear-button;
    //border: 1px dotted #000;
    text-align: center;
    font-size: 2em;
    @include button-size(18vmin, 18vmin);
    vertical-align: center;
    border-radius: 2vmin;
    //border-bottom: 5px solid lighten($clear-button, 20%);
    &:hover {
      background: lighten($clear-button, 10%);
      //border-bottom: 5px solid $clear-button;
      color: darken(#FFFFFF, 10%);
      cursor: pointer;
    }
  }
  
  #screen {
    background: $screen;
    //border: 1px dotted #000;
    text-align: right;
    padding-right: 6vmin;
    padding-left: 6vmin;
    font-size: 2em;
    @include button-size(58vmin, 18vmin);
    vertical-align: middle;
    border-radius: 2vmin;
    span {
      color: $number-button;
      font-size: 1em
    }
  }
  
  #hidden {
    position: absolute;
    visibility: hidden;
    whitespace: nowrap;
    display: block;
    background: #FFFFFF;
    color: #000000!important;
    width: auto;
  }
            
          
!
            
              /* Variables controlling the inner workings of the calculator */
var ans = 0;
var operation = "";
var comma = false;
var input = "";
var first = true;

/* Set-up for font scaling in output */
var span = document.getElementById('out');
var baseFontSize = parseFloat(window.getComputedStyle(span, null).getPropertyValue('font-size'));

/* Keyboard functionality */
var listener = new window.keypress.Listener();
document.addEventListener('keydown', (event) => {
  const keyName = event.key;
  
  switch (keyName) {
    case "1":
    case "2":
    case "3":
    case "4":
    case "5":
    case "6":
    case "7":
    case "8":
    case "9":
    case "0":
      enterNumber(keyName);
      break;
    case "+":
    case "-":
    case "*":
    case "/":
      selectOp(keyName);
      break;
    case "=":
    case "Enter":
      evaluate(ans, input, operation);
      break;
    case ".":
      toggleDecimal();
      break;
    case "Escape":
      clearAll();
      break;
    default:
      break;
  }

}, false);

/* jQuery initiation */
$(document).ready(function() {

  $("span.screen").text(ans.toString());
  $(".clear").click(function() {
    clearAll();
  });
  /* Create click events */

  // Numbers
  $(".num").click(function() {
    enterNumber($(this).text());
  });

  // Decimal
  $(".dec").click(function() {
    toggleDecimal();
  });

  // Operators
  $(".operator").click(function() {
    selectOp($(this).text());
  });

  // Eval
  $(".eval").click(function() {
    if (operation !== "") {
      evaluate(ans, input, operation);
      operation = "";
    }
  });

});

/* Calculator functions */

function enterNumber(num) {
  input += num;
  $('.screen').text(input);
}

function clearAll(ansRetain) {
  if (typeof ansRetain !== 'undefined') {
    ans = ansRetain;
  } else {
    ans = 0;
    $('.screen').text(ans);
  }
  operation = "";
  comma = false;
  input = "";
  first = false;
}

function toggleDecimal() {
  if (!comma) {
    comma = true;
    input += (input === "") ? "0." : ".";
  } else {
    //Allow for removing accidental clicks
    if (input.slice(-1) === ".")
      input = input.slice(0, -1);
  }
  $('.screen').text(input);
}

function selectOp(op) {
  if (operation !== "") {
    //Operation chosen already
    if (input === "") {
      //No number entered to perform previous operation => change operation
      operation = op;
      return;
    } else {
      //Evaluate operation
      evaluate(ans, input, operation);
      //Set next operation
      operation = op;
      //Reset current number

      $(".screen").text(ans.toString());
    }
  } else {
    operation = op;
    if (first) {
      ans = Number(input);
      first = false;
    }
    input = "";
  }

  console.log("New operation " + op + " saved " + operation);
}

function evaluate(prev, next, operator) {
  var number = Number(next);

  switch (operator) {
    case "+":
      ans += number;
      break;
    case "-":
      ans -= number;
      break;
    case "X":
    case "*":
      ans *= number;
      break;
    case "/":
      if (number === 0)
        ans = "Undefined";
      else
        ans /= number;
      break;
  }
  input = "";
  var print = "";

  if (ans !== "Undefined")
    print = (ans.toFixed(5) * 1).toString();
  else
    print = ans;

  scaleFont(print);
  $("#out").text(print);
  clearAll(ans);
}

//Various Helper functions
function isInt(n) {
  return n % 1 === 0;
}

function scaleFont(text) {
  //Baseline 
  var containerWidth = $("#screen").width();
  var baseSize = baseFontSize;
  var scaleFactor = 1.0;

  //Set up hidden for measuring purposes
  var testDiv = $("#hidden");
  testDiv.css("font-size", baseSize + "px");
  testDiv.html(text);

  //Rescaling loop
  while (testDiv.width() > containerWidth) {
    //RESIZING HAPPENS HERE
    factor = containerWidth / testDiv.width();
    baseSize *= factor;
    testDiv.css("font-size", baseSize + "px");

  }

  if (baseSize !== baseFontSize)
    $("#out").css("font-size", baseSize + "px");

  //Clear "operations"
  testDiv.html("");
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console