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. 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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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="text-center">
<div id="calc">
  <div id="label" class="text-center">Javascript Calculator</div>
  <div id="screen">0</div>
  
  <div id="buttons">
    
      <button class="red" value="ac" id="buttonAC">AC</button>
      <button class="red" value="c" id="buttonC">C</button>
      <button value="/" id="buttonDivide">&divide;</button>
      <button value="*" id="buttonMultiply">&times;</button>
      <button value="7" id="button7">7</button>
      <button value="8" id="button8">8</button>
      <button value="9" id="button9">9</button>
      <button value="-" id="buttonMinus">-</button>
    <button value="4" id="button4">4</button>
    <button value="5" id="button5">5</button>
    <button value="6" id="button6">6</button>
    <button value="+" id="buttonAdd">&plus;</button>
    <button value="1" id="button1">1</button>
    <button value="2" id="button2">2</button>
    <button value="3" id="button3">3</button>
    <button id="invisible"></button>
    <button value="0" id="zeroButton">0</button>
    <button value="." id="decimal">.</button>
    <button value="=" id="equalsButton">=</button>

  </div>
</div>
<footer class="text-center">
  Made by <a href='/kode29' target="_blank">Kyle M. Perkins</a>
</footer>

              
            
!

CSS

              
                * {
  transition: all 0.5s ease-in-out;
}
body{
  background-color: #888;
}
#calc{
  font-family: "Share Tech Mono", "Courier Sans", courier, monospace;
  border: 1px #666 solid;
  height: 445px;
  width: 300px;
  margin: auto;
  margin-top: 5%;
  border-radius: 20px;
  box-shadow: 5px 5px 50px #000;
  padding: 20px;
  background-color: #333;
  margin-bottom: 20px;
  color: #fff;
  
}
#calc div{
  border-radius: 10px;
}
#label{
  text-transform: uppercase;
  line-height: 2em;
}
#screen{
  background-color: #bcc4c7;
  width: 100%;
  color: #000;
  line-height: 150%;
  font-size: 3em;
  text-align: right;
  border: 3px #999 inset;
  padding: 5px;
  margin: 5% auto;
}
#calc button{
    width: 48px;
    margin: 2%;
    height: 45px;
    font-size: 200%;
    border-radius: 5px;
    color: #fff;
    background-color: #666;
  border: 2px transparent solid;
    border-bottom: 2px #777 outset;

}
footer{
  line-height: 3em;
  font-size: 1.1em;
}
#calc button:active, #calc button#invisible{
  border: 2px transparent solid;
  transform: translate(0, 3px);
}
#calc button:focus {outline:0;}
#calc #equalsButton{
    height: 108px;
    line-height: 90px;
    position: relative;
    top: -34px;
    margin-top: -45px;
}
#calc #zeroButton{
  width: 115px;
}
#calc button.red{
  background-color: #900;
}
              
            
!

JS

              
                $(function() {
    $(document).keydown(function(e) {
        switch (e.key) {
            case "0":
                $("#zeroButton").click();
                break;
            case "1":
                $("#button1").click();
                break;
            case "2":
                $("#button2").click();
                break;
            case "3":
                $("#button3").click();
                break;
            case "4":
                $("#button4").click();
                break;
            case "5":
                $("#button5").click();
                break;
            case "6":
                $("#button6").click();
                break;
            case "7":
                $("#button7").click();
                break;
            case "8":
                $("#button8").click();
                break;
            case "9":
                $("#button9").click();
                break;
            case "C":
            case "c":
                $("#buttonC").click();
                break;
                //case : $('#buttonAC').click(); break;
            case "-":
                $("#buttonMinus").click();
                break;
            case "+":
                $("#buttonAdd").click();
                // may mod this
                break;
            case "*":
                $("#buttonMultiply").click();
                break;
            case "/":
                $("#buttonDivide").click();
                break;
             case ".":
                event.preventDefault();
                $("#decimal").click();
                break;
            case "Enter":
            case "=":
                event.preventDefault();
                $("#equalsButton").click();
                break;
        }

        // Cover for ESC
        if (e.keyCode == 27) {
            $("#buttonAC").click();
        }
    });

    var log = "";
    var screenMsg = "";
    var answer = "";
    var currentNumber = "";
    var reset = false;
    var lastOperator = "";
    var error = false;

  function round(value, decimals) {
  return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
  
    $("#buttons button").on("click", function() {
            var entry = $(this).val();
            console.log(entry);
      
            if (screenMsg.indexOf("ERR") != -1)
              error = true;

            // First, check to see if our first entry is an operator
            // If so, add the operator to our previous answer (ex: -5 + 10)
            if ((screenMsg === "+" || screenMsg === "-" || screenMsg === "/" || screenMsg === "*") &&
                screenMsg != "0") {
                log = answer;
              console.log("Log = Answer ("+answer+")");
              console.log("Current Num: "+currentNumber);
            } else if (answer != "" && Number(entry) && reset == true) {
             
                // If we're starting over (ex: 789-987=456-654=)
               reset = false;
                log = '';
                answer = "";
            }

        // Next, check for an AC/C press
        if (entry == "ac" || entry == "c") {
            console.log("Cleared");
            // "c" = clear
            answer = '';
            currentNumber = '';
            screenMsg = "0";
            if (entry == "ac") {
                // AC = Always Clear ("clear everything")
                log = '';
                error = false;
              lastOperator = "";
            }
        } else if (error == false){
            if (entry == "." && currentNumber.indexOf(".") != -1) {
                // prevent the entering of multiple decimals  
                entry = "";
               if (screenMsg == "0" && entry == ".")
                 currentNumber = "0.";
            } else if (entry != "=") {
                // combine the numbers
              reset = false;
                currentNumber += entry;
                console.log("Current Num: " + currentNumber);
                screenMsg = currentNumber;


                if (entry === "+" || entry === "-" || entry === "/" || entry === "*") {
                  lastOperator = entry;
                    if (jQuery.inArray(log.substr(log.length - 1), ["+", "-", "*", "/"]) != -1) {
                        entry = "";
                    }
                    
                    log += currentNumber;
                    currentNumber = '';
                }
            }


            // Do the operations (use eval)
            if (entry == "=") {
              console.log("Log = "+log);
                if (answer != "") {
                    log += lastOperator + screenMsg;
                } else {
                    log += currentNumber;
                }

              // Run our calculations ONLY IF there is an operator in the log
              console.log("Last Op: "+lastOperator);
              if (lastOperator != ""){
                console.log("Calculating Answer of "+log + "("+currentNumber+")");
                 currentNumber = '';
                  answer = eval(log)+"";
                if (answer.length > 10 && answer.indexOf(".") !== -1){
                  var per = answer.indexOf(".");
                  
                  var newAnswer = round(answer, 9 - per);
                  answer = newAnswer;
                }
                console.log("Answer: "+answer);
                screenMsg = answer + "";
              }
              reset = true;
            }
        }
        
      console.log("Log: "+log);
        if (screenMsg.length > 10 || error == true)
           screenMsg = "ERR";
        $('#screen').html(screenMsg);
    });
});
              
            
!
999px

Console