css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              <link href="https://fonts.googleapis.com/css?family=Orbitron" rel="stylesheet">

<div class="container">
  <div class="row">
    <div class="calc-body col-sm-8 col-md-6 col-lg-4 offset-sm-2 offset-md-3 offset-lg-4 mx-auto my-4 p-4">
      <div class="row">
        <div class="col-sm-12">
          <h4 class="text-center">JavaScript Calculator</h4>
        </div>
      </div> <!-- /heading row -->
      
      <div class="row">
        <div class="col-sm-12 my-3 calc-screen">
          <p id="numbers">0</p>
          <p id="test"></p>
        </div>
      </div>  <!-- /calc-screen row -->
      
      <div class="row my-3">
        <div class="col-3 px-1">
          <button type="button" class="all-clear btn btn-default btn-block py-3">AC</button>
        </div>
        <div class="col-3 px-1">
          <button type="button" class="clear-entry btn btn-default btn-block py-3">CE</button>
        </div>
        <div class="col-3 px-1">
          <button type="button" class="squared btn btn-default btn-block py-3">X<sup>2</sup></button>
        </div>
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 sym">/</button>
        </div>
      </div>  <!-- /btn row 1 -->
      
      <div class="row my-3">
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 num">7</button>
        </div>
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 num">8</button>
        </div>
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 num">9</sup></button>
        </div>
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 sym">*</button>
        </div>
      </div>  <!-- /btn row 2 -->
      
      <div class="row my-3">
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 num">4</button>
        </div>
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 num">5</button>
        </div>
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 num">6</sup></button>
        </div>
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 sym">-</button>
        </div>
      </div>  <!-- /btn row 3 -->
      
      <div class="row mt-3">
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 num">1</button>
        </div>
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 num">2</button>
        </div>
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 num">3</button>
        </div>
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 sym">+</button>
        </div>
      </div>  <!-- /btn row 4 -->

      <div class="row mt-3">
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 num">.</button>
        </div>
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 num">0</button>
        </div>
        <div class="col-3 px-1">
          <button type="button" class="ans btn btn-default btn-block py-3">Ans</button>
        </div>
        <div class="col-3 px-1">
          <button type="button" class="btn btn-default btn-block py-3 sym">=</button>
        </div>
      </div>  <!-- /btn row 5 -->
      
    </div> <!-- /calc-body -->
  </div> <!-- /row -->
</div> <!-- /container -->

            
          
!
            
              html, body {
  width: 100%;
  height: 100%;
}

body {
  background: url("http://erincpotter.net/assets/math1.jpg");
  font-family: 'Orbitron', sans-serif;
}

.calc-body {
  background: #f26e70;
  border-radius: 15px;
  border: 1px solid #ef4346;
  border-right: 3px solid #ef4346;
  border-bottom: 5px solid #ef4346;
  box-shadow: 3px 5px 0px #41babe;
  min-width: 359px;
  max-width: 360px;
  color: #ecf8f8;
}

.calc-screen{
  border-radius: 10px;
  border-right: 3px solid #c7eaea;
  border-bottom: 5px solid #c7eaea;
  box-shadow: 1px 3px 0px #ef4346;
  background: #ecf8f8;
  height: 50px;
  color: #2b80ba;
  text-align: right;
  font-size: 1.5em;
  line-height: 50px;
}

#test{
  position: absolute;
  top: -9999px;
  left: -9999px;
}

.btn {
  position: relative;
  border-radius: 10px;
  border-right: 2px solid #f9bb1f;
  border-bottom: 5px solid #f9bb1f;
  box-shadow: 1px 3px 0px #ef4346;
  background: #fbcf63;
  font-family: 'Orbitron', sans-serif;
  font-weight: bold;
  color: #2b80ba;
  font-size: 1em;
  transition: 0.3s;
}

.btn.focus, .btn:focus {
  box-shadow: 1px 3px 0px #ef4346;
}

.btn:active,
.activate {
  box-shadow: 1px 1px 0px #ef4346;
  top: 2px;
}


            
          
!
            
              $(document).ready(function() {
  var input = "";
  var allInput = "0";
  var ans = "";
  var digits = /\d/g;
  var mathSym = /[-+*/]/g;
  
  function calculate() {
    function testWidth(){
      $('#test').html(allInput);
      var testWid = $('#test').outerWidth();
      var screenWid = $('.calc-screen').width();

      if(testWid > screenWid){
        return false;
      } else {
        return true;
      }
    }

    //reset allInput if last calculate caused an error
    if(allInput.match(/[A-Za-z]/) != null && input !== "ce"){
      allInput = "0";
    }
    
    var lastInput = allInput.slice(-1);
    
    if (input === "="){
      //remove stray math symbols at the end (not followed by numbers)
      if( lastInput.match(mathSym) != null ){
        allInput = allInput.slice(0, -1);
      } 

      //evaluate the string as a numerical equation
      allInput = eval(allInput).toString();
      
      //deal with of some of the float issues
      if(allInput.indexOf('.') !== -1){
        var lastNum;
        while(testWidth() === false){
          lastNum = parseInt(allInput.charAt(allInput.length-1));
          allInput = allInput.slice(0, -1);
        }
        
        var int = allInput.slice(0, allInput.indexOf('.')+1);
        var decimal = allInput.slice(allInput.indexOf('.')+1);
        if(decimal.indexOf('00000') !== -1){
          decimal = decimal.slice(0, decimal.indexOf('00000'));
        } else if (decimal.indexOf('99999') !==-1){
          decimal = (parseInt(decimal.slice(0, decimal.indexOf('99999'))) + 1).toString();
        } else if(decimal.length > 5){
          if(lastNum >= 5){
            var last = (parseInt(decimal.charAt(decimal.length-1))+1).toString();
            decimal = decimal.slice(0, -1) + last;
          }  
        }
        allInput = int + decimal;
      }

      if(testWidth()){
        //save answer for ans button
        ans = allInput;
      } else {
        //send an error if allInput is too long
        allInput = "error";
      }

    } else {
      //make sure the first entry isn't a symbol (except -)
      if(allInput === "0" && input.match(mathSym) != null && input !== "-"){
        allInput = "0";
        input = "0";
      }
      
      //make sure there aren't multiple symbols in a row
      if((lastInput.match(mathSym) != null && input.match(mathSym) != null && input !== "-") ||       
         (lastInput === "." && input === ".") ||
         (lastInput === "-" && input === "-" ) ){
          input = "";
      }
      
      //don't allow dividing by 0
      if(lastInput === "/" && input === "0" ){
        input = "";
      }
      
      //make sure there is only one dot in a number
      var lastDot = allInput.lastIndexOf('.');
      if(input === "." && lastDot != -1){
        var allSym = allInput.match(mathSym);
        
        if(allSym == null){
          input = "";
        } else {
          var lastSym = allInput.lastIndexOf(allSym[allSym.length-1]);
          if(lastDot > lastSym){
            input = "";
          }
        }
      }

      //get rid of the clear entry input
      if(input === "ce"){
        input = "";
      }

      if(allInput === "0"){
        allInput = input;
      } else {
        allInput += input;
      }

      while(testWidth() === false){
        allInput = allInput.slice(0, -1);
      }
    }
    
    //display string on the calc-screen
    $("#numbers").html(allInput);
    input = "";
  }
  


  //All clear button
  function allClear(){
    //reset allInput to empty string
    allInput = "0";
    input = "0";
  }
  


  //Clear entry button
  function clearEntry() {
    //slice off last character
    if(allInput.length > 1){
      allInput = allInput.slice(0, -1);
      input = "ce";
    } else {
      allInput = "0";
      input = "0";
    }
  }



  function squared() {
    var lastNum;
    var allSym = allInput.match(mathSym);
    
    if(allSym == null){
      allInput += "*" + allInput;
      input = "=";
    } else {
      //get index of last math symbol in allInput
      var lastIdx = allInput.lastIndexOf(allSym[allSym.length-1]);
      
      if(lastIdx === 0){
        //get all characters if the num is negative
        lastNum = parseFloat(allInput);
        //reset allInput
        allInput = "";
        //reset input to lastNum squared
        input = (lastNum * lastNum).toString();
      } else {
        //get all characters after last math symbol
        lastNum = parseFloat(allInput.slice(lastIdx+1));
        //remove lastInput, square it, and add back to allInput
        allInput = allInput.slice(0, lastIdx+1);
        input = (lastNum * lastNum).toString();
      }
    }
  }



  //Collect user input on click and calculate
  $('button').click(function() {
    if($(this).hasClass('num') || $(this).hasClass('sym')){
      input = $(this).text();
    } else if($(this).hasClass('all-clear')){
      allClear();

    } else if($(this).hasClass('clear-entry')){
      clearEntry();

    } else if($(this).hasClass('ans')){
      input = ans;

    } else if($(this).hasClass('squared')){
      squared();
    }

    calculate();
  });
  
  

  //Collect user input on keypress and calculate
  //(must be keypress to allow for math symbols that require shift)
  $(document).keypress(function(event){
    event.preventDefault();
    if(event.which === 13){
      input = "=";
    } else {
      input = String.fromCharCode(event.which);

      if(input.match(digits) == null && 
         input.match(mathSym) == null &&
         input.match('.') == null){
        input = "";
      }
    }
    calculate();
  });

  //Call clearEntry & allClear on backspace/delete keydown
  //(must be keydown to include backspace)
  $(document).keydown(function(event){
    if(event.which == 8){
      clearEntry();
    } else if(event.which == 46) {
      allClear();
    }
    calculate();
  });
  
}); //end document.ready
            
          
!
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.
Loading ..................

Console