Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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 id="calculator">
<!--   <p id="output"></p> -->
  <input type="text" disabled id="output"/>
<div id="keypad">
<button class="clear">C</button>  
<button class="number">1</button>
<button class="number">2</button>
<button class="number">3</button>
<button>/</button>
  
<button class="number">4</button>
<button class="number">5</button> 
<button class="number">6</button>
<button>*</button>

<button class="number">7</button>
<button class="number">8</button>
<button class="number">9</button>
<button>-</button>

<button class="number">0</button> <button class="number">.</button>
<button>=</button>
<button>+</button>
</div>  
</div>
              
            
!

CSS

              
                body {
    height: 100%;
  text-align: center;
  display: -webkit-box;
  display: flex;
  align-content: center;
  margin-top: 24px;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
          flex-flow: column wrap;
  background: #f2f2f2;
}
#calculator {
  padding: 12px 10px 6px;
    border-radius: 2px;
    box-shadow: 0px -6px 10px white, 0px 4px 15px rgba(0, 0, 0, 0.15);
    width: 350px;
}
#calculator #output {
      height: 60px;
    font: bold 30px/44px monospace;
    margin-bottom: 28px;
    text-align: right;
    border-radius: 2px;
    white-space: nowrap;
    overflow: hidden;
    margin-right: 8px;
    box-shadow: inset 2px 2px 5px #BABECC, inset -5px -5px 10px #FFF;
    width: 100%;
    box-sizing: border-box;
    transition: all 0.2s ease-in-out;
    appearance: none;
    -webkit-appearance: none;
}
#calculator #keypad {
 width: 350px;
    height: 410px;
    display: grid;
    grid-template-columns: repeat(4, 80px);
    -webkit-box-pack: justify;
    justify-content: space-between;
}
#calculator #keypad button {
  position: relative;
    width: 70px;
    height: 70px;
    border-radius: 50%;
    background: #f2f2f2;
    transition: all 100ms cubic-bezier(0.175, 0.885, 0.32, 1.275);
    box-shadow: 0px -6px 10px white, 0px 4px 15px rgba(0, 0, 0, 0.15);
    cursor: pointer;
}
#calculator #keypad button:active {
  box-shadow: 0 15px 20px rgba(0, 0, 0, 0.02);
}
#calculator #keypad button.clear {
  grid-column: 1 / span 4;
  grid-row: 1 / span 1;
  background: #FFE1D9;
}
button:focus {
    border: none;
    outline: 0 !important;
    outline-style: none;
}
button, #output {
    border: 0;
    outline: 0;
    font-size: 16px;
    border-radius: 320px;
    padding: 16px;
    background-color: #EBECF0;
    text-shadow: 1px 1px 0 #FFF;
}
button:active:after {
    box-shadow: inset 0px -2px 5px white, inset 0px 2px 5px rgba(0, 0, 0, 0.15);
}
button:after {
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    z-index: 2;
}
              
            
!

JS

              
                (function myCalculator() {
/*Hook up buttons*/
  //Save the buttons that make up the calculator keys as an array
  const keys = document.getElementsByTagName("button");
  //Loop through them and assign a click handler so something happens when they are clicked. Using ECMA6 "for...of" instead of older indexed loop.
  for (const key of keys) {
    key.onclick = handleClick;
  }
//Declare and initialize variables
  //Save the output paragraph as a variable because I'm going to use it constantly
  const output = document.getElementById("output");
  let numOut = "", //number input before operator
    numOutNew = "", //number input after operator
    op = "", //operator
    int = 0; //calculation result;
 
  //Click function for the buttons
    function handleClick() {
    let num = this.innerText; //when a button is pressed we get its value, which could be a number, a math operator or the "C" key, which clears the calculator
    if (this.classList.contains("number")) {//Input is a number or the decimal point
      numOut += num; //as long as user is not pressing an operation key we concatenate the input to keep building the number with each digit entered
      output.value = numOut; //then send it to the output screen
    } else {
      //The user has chosen a calculation, so we do run the calculation function
      doCalc(num);//"num" here is an operator, like "*" or "C"
    }
  }
//When the calculation is finished or if the math breaks from bad key input we have to clear the calculator
  function doClear() {
    output.value = "";
    numOut = "";
    numOutNew = "";
    op = "";
    int = 0;
  }

  //Calculation function
  function doCalc(calc) {
    //pass in an operator This was the "num" value from the handleClick function
    if (calc === "C") {//clear calculator
      doClear();
    } 
 // numOutNew is assigned the value of the first number the user inputs AFTER an operator key is pressed and this function runs. If it is empty, then this is the first time we're getting an operator and we don't have enough numbers to do math. If it is not empty we are have received the second number and a second operator has been clicked, "=" for example.
    else if (numOutNew !== "") {
      numOut = parseFloat(numOut); //convert input from string to number
      switch (op) {
        case "/":
          if (numOut !== 0) {
            int = numOutNew / numOut;//divide
          } else {
            int = "error";//division by zero is not allowed
          }
          break;
        case "*":
          int = numOutNew * numOut;//multiply
          break;
        case "-":
          int = numOutNew - numOut;//subtract
          break;
        case "+":
          int = numOutNew + numOut;//add
          break;
        case "=":
          int = parseFloat(output.value);//When "=" is clicked the previous operator's calculation is done and we just have to save that result for next time, in case the user continues to do more operations to their total. The result of the previous operation was put in the output field, so we get it from there.
          break;
      }
      if (isNaN(int)) {//if we tried to do an operation without two numbers, for example ". / 3"
        output.value = "error";//show the user "error" instead of garbage output
            setTimeout(function() {
              doClear();
            }, 2000);//does doClear after 2 seconds and the error message disappears
      } else {
        output.value = int; //Show the result
        numOutNew = int; //Save the result as the first number for the next calculation if the user chains calculations
      }
    } else if (numOut !== "") {
      //If I didn't have to doClear for a NaN result I will have a numeric value
      numOutNew = parseFloat(numOut); //The user has used an operator for the first time and we need to store the first number input until we get the second one and can use the operator
    }
    op = calc; //We store the operator that was passed into this function to use after the we get the second number
    numOut = ""; //Clear the input variable to reuse for the next number
  }

})();

              
            
!
999px

Console