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.

            
              <div id="calc">
  <div id="logo">
    <h3>Sir Calculot</h3>
  </div>
  <div id="display">
  </div>
  <div class="buttons">
    <button class="numbers" id="clearall">AC</button>
    <button class="numbers" id="clear">CE</button>
    <button style="font-size: 20px;" class="operators">%</button>
    <button style="font-size: 25px;" class="operators">/</button>
    <button class="numbers">7</button>
    <button class="numbers">8</button>
    <button class="numbers">9</button>
    <button style="font-size: 35px; padding-top:14px;" class="operators">*</button>
    <button class="numbers">4</button>
    <button class="numbers">5</button>
    <button class="numbers">6</button>
    <button style="font-size: 40px; padding-bottom:30px;" class="operators">-</button>
    <button class="numbers">1</button>
    <button class="numbers">2</button>
    <button class="numbers">3</button>
    <button style="font-size: 30px; padding-bottom:10px;" class="operators">+</button>
    <button class="numbers">0</button>
    <button style="font-size: 20px; padding-bottom:7px;" class="numbers" id="point">.</button>
    <button style="padding-bottom:8px" id="equals">=</button>
  </div>
</div>
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Inconsolata);
@import url(https://fonts.googleapis.com/css?family=Aldrich);

* {
   -moz-user-select: -moz-none;
   -khtml-user-select: none;
   -webkit-user-select: none;

   /*
     Introduced in IE 10.
     See https://ie.microsoft.com/testdrive/HTML5/msUserSelect/
   */
   -ms-user-select: none;
   user-select: none;
}

html {
  background: white;
  background: radial-gradient(circle, #fff 20%, #ccc);
  background-size: cover;

}

h3 {
  font-family: "Aldrich";
  text-align: center;
  font-size: 22px;
  color: #F0F4F5;
}

#logo {
  padding: 5px 0 5px 0;
}

#calc {
  background: #62c2cc;
  width: 280px;
  height: 408px;
  margin-bottom: 5px;
  margin: 50px auto;
  padding:10px;
  box-shadow: 0px 4px #009de4, 0px 10px 15px rgba(0, 0, 0, 0.2);
  border-radius: 6px;
  box-shadow: 0 0 20px #545454;
}

#display {
  width: 270px;
  height: 55px;
  line-height: 55px;
  margin-top: 5px;
  margin-bottom: 15px;
  padding: 0px 5px;
  background-color: #D8EDE2;
  text-align: right;
  font-size: 35px;
  font-family: Courier, monospace;
  border: 0px solid black;
  border-radius: 5px;
  box-shadow: inset 0px 6px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  color: #5A665F;
}

.buttons {
  margin: auto 0;

}

button {
  margin: 0;
  display: block;
  width: 70px;
  height: 60px;
  float: left;
  padding: 2px;
  margin: 5px, 6px;
  text-align: center;
  text-decoration: none;
  background: #eef66c;
  border-radius: 5px;
  outline: none;
  font-size: 15px;
}

button.numbers {
  font: bold 14px Arial, sans-serif;
  color: #7889B0;
}

button.operators {
  background: #fdb813;
  color: white;
  font-size: 17px;
}

button:hover {
  background: #e9f33f;
}

button:hover.operators {
  background: #e3a002;
}

button:focus.operators {
    border: 4px solid #f17022;
}

button#clear, button#clearall {
  background: #e4f6f8;
}
button:hover#clear, button:hover#clearall {
  background: #c2ebef;
}

button#equals {
  background: #f17022;
  color: white;
  font-size: 30px;
  width: 140px;

}

button:hover#equals {
  background: #eb620f;
}

            
          
!
            
              $(document).ready(function(){

    $("#display").text("0");
    var number = "";
    var operator = "";
    var sumString = "";
    var wipe = false;
    var opCount = 0;

    function lastCharAnOperator() {
        if (sumString[sumString.length-1] == "*" || sumString[sumString.length-1] == "/" || sumString[sumString.length-1] == "%" || sumString[sumString.length-1] == "+" || sumString[sumString.length-1] == "-") {
            return true;
        }
    }

    function largeAnswerTrim(n) {
        if (n.toString().length > 13) {
            return n.toPrecision(8);
        } else {
            return n;
        }
    }

    function truncateIfTooLong(n) {
        if (n.toString().length > 13) {
            console.log("firing")
            return n.substring(0, 13);
        } else {
            return n;
        }
    }

  // decimal listener
    $("#point").click(function(){
        if (number.indexOf(".") === -1) {
            // no previous decimals, append
            number += "."
            $("#display").text(number);
        }
    });

    // Number listeners
    $(".numbers").not("#clear, #clearall, #point").click(function() {
        if (wipe) {
            number = "";
            $("#display").text("0");
            wipe = false;
        }
        number += $(this).html();
        number = truncateIfTooLong(number);
        $("#display").text(number);
    });

    // Operator listeners
    $(".operators").click(function() {
        try {
            opCount += 1;
            wipe = false;
            operator = $(this).text();
            sumString += number;
            number = "";
            // intermediate calc
            if (opCount > 1) {
                var answer = eval(sumString);
                answer = largeAnswerTrim(answer);
                $("#display").text(answer);
            }
            sumString += operator;
            console.log(sumString);
            // if last item in item in sumString is an operator, replace with new opp
        }
        catch(err) {
            console.log("Incorrect operator placement")
            if (lastCharAnOperator()) {
                console.log("Operator switch");
                sumString = sumString.slice(0, -1);
                sumString += operator;
            }
        }
    });

    // Clear and clearall listeners
    $("#clear,#clearall").click(function(){
        number = "";
        operator = "";
        $("#display").text("0");
        if ($(this).attr("id") === "clearall") {
            sumString = "";
        }
    });

    // Equals listener
    $("#equals").click(function(){
        opCount = 0;
        sumString += number;
        if (lastCharAnOperator()) {
            sumString = sumString.slice(0, -1);
        }
        var answer = eval(sumString);
        // if answer.length > 13
        // exponent

        answer = largeAnswerTrim(answer);

        console.log(answer);
        $("#display").text(answer);
        // handle any division by zero
        if (answer == "Infinity") {
            $("#display").text("Error");
        }
        operator = "";
        sumString = "";
        number = answer;
        wipe = true;
    });

});
            
          
!
999px
Loading ..................

Console