cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <!-- hyperlinks: target="_blank" for codepen.io -->
  <div class="container h-100">
    <div class="row h-100 justify-content-center align-items-center">
      <div class="col-11 col-sm-8 col-md-7 col-lg-5 col-xl-4">
        <div class="container pt-2 pb-5 px-4-5 pb-4-5 calculator-body">

          <div class="row px-1 pb-2">
            <div class="col-12 text-center mb-2 text-white title">
              A Simple Calculator
            </div>
            <div class="col-12 screen text-right rounded p-3">
              <h1 id="topDisplay">0</h1>
              <h5 id="bottomDisplay" class="text-muted mx-1 my-0">0</h5>
            </div>
          </div>

          <div class="row">
            <div class="col-3 p-1">
              <button class="btn btn-danger w-100" value="A">
                AC
              </button>
            </div>
            <div class="col-3 p-1">
              <button class="btn btn-danger w-100" value="E">
                CE
              </button>
            </div>
            <div class="col-3 p-1">
              <button class="btn btn-inverse w-100" value="/">
                ÷
              </button>
            </div>
            <div class="col-3 p-1">
              <button class="btn btn-inverse w-100" value="*">
                x
              </button>
            </div>

            <div class="col-3 p-1">
              <button class="btn btn-inverse w-100" value="7">
                7
              </button>
            </div>
            <div class="col-3 p-1">
              <button class="btn btn-inverse w-100" value="8">
                8
              </button>
            </div>
            <div class="col-3 p-1">
              <button class="btn btn-inverse w-100" value="9">
                9
              </button>
            </div>
            <div class="col-3 p-1">
              <button class="btn btn-inverse w-100" value="-">
                -
              </button>
            </div>

            <div class="col-3 p-1">
              <button class="btn btn-inverse w-100" value="4">
                4
              </button>
            </div>
            <div class="col-3 p-1">
              <button class="btn btn-inverse w-100" value="5">
                5
              </button>
            </div>
            <div class="col-3 p-1">
              <button class="btn btn-inverse w-100" value="6">
                6
              </button>
            </div>
            <div class="col-3 p-1">
              <button class="btn btn-inverse w-100" value="+">
                +
              </button>
            </div>
          </div>

          <div class="row">
            <div class="col-6">
              <div class="row">
                <div class="col-6 p-1">
                  <button class="btn btn-inverse w-100" value="1">
                    1
                  </button>
                </div>
                <div class="col-6 p-1">
                  <button class="btn btn-inverse w-100" value="2">
                    2
                  </button>
                </div>
              </div>
              <div class="row">
                <div class="col-12 p-1">
                  <button class="btn btn-inverse w-100" value="0">
                    0
                  </button>
                </div>
              </div>
            </div>

            <div class="col-3">
              <div class="row">
                <div class="col-12 p-1">
                  <button class="btn btn-inverse w-100" value="3">
                    3
                  </button>
                </div>
                <div class="col-12 p-1">
                  <button class="btn btn-inverse w-100" value=".">
                    .
                  </button>
                </div>
              </div>
            </div>

            <div class="col-3 p-1 d-flex align-items-stretch">
              <button class="btn btn-inverse w-100" value="=">
                =
              </button>
            </div>

          </div>
        </div>

        <div class="text-white text-center mt-3 w-100">
          <p>Designed and Coded by <a class="text-warning" href="http://www.gregwong.me" target="_blank">Greg Wong</a></p>
        </div>
      </div>
    </div>
  </div>
            
          
!
            
              html, body {
  height: 100%;
  background-color: black;
  font-family: "PT Mono","Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;
}

.title {
  font-family: Roboto,"Helvetica Neue",Arial,sans-serif;
}

.px-4-5 {
  padding-left: 2.5rem!important;
  padding-right: 2.5rem!important;
}

.pb-4-5 {
  padding-bottom: 2.3rem!important;
}

.calculator-body {
  border-radius: 20px;
  background-color: gray;
  box-shadow: 0px 0px 1px 0px white, inset 0px 0px 15px 0px black;
}

.screen {
  background-color: #c3c2ab;
}

.btn-inverse {
  color: #fff;
  background-color: #333;
}

.btn-inverse:hover:hover {
  color: #fff;
  background-color: #222;
  border-color: #adadad;
}

/* Modifications to Bootstrap classes */
.btn {
  font-family: "PT Mono","Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;
  box-shadow: inset 0px 1px 7px 0px gray;
  border-style: none;
  -webkit-transition-duration: 0s;
  -moz-transition-duration: 0s;
  -o-transition-duration: 0s;
  transition-duration: 0s;
}

.btn:active {
  box-shadow: inset 0px 0px 1px 3px white!important;
}

.btn:focus {
  /* to modify default behavior of outlining buttons after they've been clicked */
  outline: none!important;
  box-shadow: inset 0px 1px 7px 0px gray;
}

.btn-danger {
  box-shadow: inset 0px 1px 7px 0px #e07774;
}
/* End of Modifications to Bootstrap classes */

            
          
!
            
              // extra function to access the last element of an array
if (!Array.prototype.peek){
  Array.prototype.peek = function(){
    return this[this.length - 1];
  };
};

var Calculator = function(){

  const TOP_LENGTH_LIMIT = 10;
  const TOP_DIGIT_LIMIT = 8;
  const BOTTOM_LENGTH_LIMIT = 20;
  const DIGIT_LIMIT_REACHED_MESSAGE = "Digit Limit Reached";
  var stack = ['0'];

  this.keyPress = function(input) {
    // returns an array of two strings if operating normally, otherwise returns undefined
    if (typeof input != "string") {
      console.error("typeof input is expected to be 'string', but was '" + typeof input + "'");
      return undefined;
    }
    if (!isValidInput(input)) {
      console.error("input not valid, I don't know what to do with it: '" + input + "'");
      return undefined;
    }

    if (input == "E") { // clear entry
      clearEntry();
    }
    else if (input == "A"){ // clear all
      clearAll();
    }
    else { // input is digit, decimal, operation, or equal sign
      var last = stack.peek();
      if (isOneOrMoreDigits(last)) {
        if (isSingleDigit(input)) {
          var popped = stack.pop();
          if (popped == '0') { // to ensure 0 is not repeated in the beginning
            stack.push(input);
          }
          else {
            stack.push(last + input);
          }
        }
        else if (isDecimal(input)) {
          var popped = stack.pop();
          stack.push(last + input);
        }
        else if (isOperation(input)) {
          stack.push(input);
        }
        else if (isEqualSign(input)) {
          stack.push(input);
        }
      }
      else if (isFloat(last)) {
        // Don't use eval() for floats. We want to keep the extra . if it's included (e.g., 2. instead of 2)
        if (isSingleDigit(input)) {
          var popped = stack.pop();
          stack.push(last + input);
        }
        else if (isOperation(input)) {
          stack.push(input);
        }
        else if (isEqualSign(input)) {
          stack.push(input);
        }
        // do nothing if isDecimal

      }
      else if (isOperation(last)) {
        if (isSingleDigit(input)) {
          if (last == '/' && input == '0') {
            // do nothing, as division by zero is not allowed
          }
          else {
            stack.push(input);
          }
        }
        else if (isDecimal(input)) {
          stack.push('0.');
        }
        // do nothing if isOperation or isEqualSign
      }
      else if (isEqualSign(last)) {
        if (isSingleDigit(input)) {
          // empty stack and add input to it
          stack = [input];
        }
        else if (isDecimal(input)) {
          // empty stack and add '0.' to it
          stack = ['0.'];
        }
        else if (isOperation(input)) {
          result = calculateTopValue();

          // empty stack except for the result from the last calculation and add the operation
          stack = [result, input];
        }
        // do nothing if isEqualSign
      }
    }

    return getDisplayValues();
  };

  function calculateTopValue() {
    // assume last value in stack is '='
    var string = stack.slice(0,stack.length-1).join(''); // without last '='
    var rawResult = eval(string);
    var digits = rawResult >= 0 ? TOP_DIGIT_LIMIT : TOP_DIGIT_LIMIT - 1;
    return eval(eval(string).toPrecision(digits)).toString();
  }

  function getDisplayValues() {
    var topDisplay = stack.peek();
    var bottomDisplay = stack.join('');

    if (isEqualSign(topDisplay)){
      topDisplay = calculateTopValue();
      bottomDisplay += topDisplay;
    }

    // check if screen can still accommodate displayed text
    if (digitLimitReached(topDisplay, bottomDisplay)) {
      clearAll();
      topDisplay = stack.peek();
      bottomDisplay = DIGIT_LIMIT_REACHED_MESSAGE;
    }

    // for people who are used to 'x' over '*'
    topDisplay = topDisplay.replace("*", "x");
    bottomDisplay = bottomDisplay.replace(/\*/g, "x");

    return [topDisplay, bottomDisplay];
  }

  function clearAll() {
    stack = ['0'];
  }

  function clearEntry() {
    if (stack.length <= 1) {
      clearAll();
    }
    else {
      stack.pop();
    }
  }

  function digitLimitReached(topDisplay, bottomDisplay) {
    return (topDisplay.length > TOP_LENGTH_LIMIT || bottomDisplay.length > BOTTOM_LENGTH_LIMIT);
  }

  function isSingleDigit(str) {
    return str.match(/^\d$/) !== null;
  }

  function isOneOrMoreDigits(str) {
    return str.match(/^\d+$/) !== null;
  }

  function isFloat(str) {
    // does not take into account negative floats
    return str.match(/^\d+[.]\d*$/) !== null;
  }

  function isDecimal(str) {
    return str == '.';
  }

  function isOperation(str) {
    return str.match(/^[\+\-\*\/]$/) !== null;
  }

  function isEqualSign(str) {
    return str == '=';
  }

  function isValidInput(str) {
    return str.match(/^[0-9.\+\-\*\/=AE]$/);
  }
};

$(document).ready(function() {
  var myCalc = new Calculator();

  $(".btn").click(function () {
    var displayValues = myCalc.keyPress($(this).val());
    $("#topDisplay").text(displayValues[0]);
    $("#bottomDisplay").text(displayValues[1]);
  });
});

/* Test Suite */
/* ********** */

// uncomment to run test suite
// runTests();

function runTests() {

  var myTestCalc = new Calculator();
  const DIGIT_LIMIT_REACHED_MESSAGE = "Digit Limit Reached";
  const MY_TEST_CASES = [
    // [input, expected top display, expected bottom display]
    [
      ['A','0','0'],
      ['1+1=','2'],
      ['1+2=','3'],
      ['1+3=','4'],
      ['1+4=','5'],
      ['1+2+3+4+5=','15'],
      ['000000','0','0'],
      ['000000=','0','0=0'],
    ],
    [
      ['A','0','0'],
      ['AAAAAAA','0','0'],
      ['EEEEEEE','0','0'],
    ],
    [
      ['A','0','0'],
      ['1+1=','2'],
      ['1+2=','3'],
      ['1+3=','4'],
      ['1+4=','5'],
      ['1+2+3+4+5=','15'],
      ['000000','0','0'],
      ['000000=','0','0=0'],
    ],
    [
      ['A','0','0'],
      ['0=','0','0=0'],
      ['+2=','2','0+2=2'],
      ['+3=','5','2+3=5'],
    ],
    [
      ['A','0','0'],
      ['1+2+3+4','4','1+2+3+4'],
      ['E','+','1+2+3+'],
      ['E','3','1+2+3'],
      ['E','+','1+2+'],
      ['E','2','1+2'],
      ['E','+','1+'],
      ['E','1','1'],
      ['E','0','0'],
    ],
    [
      ['A','0','0'],
      ['1/9','9','1/9'],
      ['=','0.11111111','1/9=0.11111111'],
      ['=','0.11111111','1/9=0.11111111'],
      ['=','0.11111111','1/9=0.11111111'],
      ['1/9+1/9','9','1/9+1/9'],
      ['=','0.22222222','1/9+1/9=0.22222222'],
    ],
    [
      ['A','0','0'],
      ['1234567890','1234567890','1234567890'],
      ['*10','10','1234567890x10'],
      ['=','0', DIGIT_LIMIT_REACHED_MESSAGE],
      ['=','0', '0=0'],
      ['12345678901', '0', DIGIT_LIMIT_REACHED_MESSAGE],
      ['123456789011', '1', '1'],
      ['A','0','0'],
      ['1234567890112', '12', '12'],
      ['A','0','0'],
      ['1+1+1+1+1+1+1+1+1+1+','+','1+1+1+1+1+1+1+1+1+1+'],
      ['A','0','0'],
      ['1+1+1+1+1+1+1+1+1+1+1','0',DIGIT_LIMIT_REACHED_MESSAGE],
    ],
  ];

  var errorCount = 0;
  for (var i = 0; i < MY_TEST_CASES.length; i++) {
    var testStrings = MY_TEST_CASES[i];
    console.log("Running test " + (i+1) + " of " + MY_TEST_CASES.length);
    for (var j = 0; j < testStrings.length; j++) {
      var testString = testStrings[j];
      var input = testString[0];
      var expectedTop = testString[1];
      var expectedBottom = testString[2];
      var output;
      for (var k = 0; k < input.length; k++) {
        output = myTestCalc.keyPress(input[k]);
      }
      var actualTop = output[0];
      var actualBottom = output[1];
      // comparing against single number result
      if (actualTop != expectedTop) {
        console.error("Failed test (single number result): " + expectedTop);
        console.log("Input: " + input);
        console.log("Expected single number result: " + expectedTop);
        console.log("Actual Top Display: " + actualTop);
        errorCount++;
      }
      // comparing against expected bottom display (if available)
      if (expectedBottom) {
        if (expectedBottom != actualBottom) {
          console.error("Failed test (expected bottom display): " + expectedBottom);
          console.log("Input: " + input);
          console.log("Expected Bottom Display: " + expectedBottom);
          console.log("Actual Bottom Display: " + actualBottom);
          errorCount++;
        }
      }
      // no expected bottom display given; comparing against generated bottom display
      else {
        if (actualBottom != input+expectedTop) {
          console.error("Failed test (generated bottom display): " + input+expectedTop);
          console.log("Input: " + input);
          console.log("Expected Combined Output: " + input+expectedTop);
          console.log("Actual Bottom Display: " + actualBottom);
          errorCount++;
        }
      }
    }
  }
  console.log("Finished running " + MY_TEST_CASES.length + " tests with " + errorCount + " total errors.");
};

            
          
!
999px
Loading ..................

Console