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

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 id = "container">
  <div id = "calculator">
    <div class = "name">
      Basic Calculator
    </div>
    <div id = "display">
      <div id = "entry">
        0
      </div>
      <div id = "history">
        
      </div>
    </div>
    <div class = "button-container">
      <button class="button red" value = "CE"> CE </button>
      <button class="button red" value = "AC"> AC </button>
      <button class="button" value ="*"> x </button>
      <button class="button" value ="/"> &divide; </button>
      <button class="button" value ="7"> 7 </button>
      <button class = "button" value ="8"> 8 </button>
      <button class = "button" value = "9"> 9 </button>
      <button class = "button" value = "-"> - </button>
      <button class = "button" value = "4"> 4 </button>
      <button class = "button" value = "5"> 5 </button>
      <button class = "button" value ="6"> 6 </button>
      <button class = "button" value ="+"> + </button>
      <button class = "button" value = "1"> 1 </button>
      <button class = "button" value = "2"> 2 </button>
      <button class = "button" value = "3"> 3 </button>
      <button class = "button tall" value = "="> = </button>
      <button class = "button wide" value ="0"> 0 </button>
      <button class = "button clear-float" value ="."> . </button>
      </div>
  </div>
</div>
              
            
!

CSS

              
                body {
  background: #529314;
  font-size: 100%;
}

#container {
  height: 100%;
  display: block;
}

#calculator {
  padding: 5px 5px;
  position: relative;
  top: 20vh;
  margin: 0 auto;
  width: 350px;
  height: 465px;
  background: #848484;
  border-radius: 4px;
  box-shadow: 0 2px 5px black, 0 -3px 2px rgba(255,255,255,.2) inset, -3px 0px 2px rgba(255,255,255,.2) inset, 3px 0px 2px rgba(255,255,255,.2) inset, 0 3px 2px rgba(255,255,255,.2) inset;
}

#display {
  box-shadow: 0 -3px 2px rgba(0,0,0,.2) inset, -3px 0px 2px rgba(0,0,0,.2) inset, 3px 0px 2px rgba(0,0,0,.2) inset, 0 3px 2px rgba(0,0,0,.2) inset;
  font-family: Consolas;
  width: auto;
  height: 80px;
  background: #d6c6ad;
  border-radius: 3px;
}

.name {
  text-align: right;
  padding-right: 5px;
  color: white;
  margin: auto 0;
  font-family: Helvetica;
  font-size: 1.5em;
  margin-bottom: 3px;
}

#entry {
  height: 70%;
  letter-spacing: 6px;
}

#entry, #history {
  font-size: 3.5em;
  text-align: right;
  width: auto;
  padding: 2px 5px;
}

#history {
  letter-spacing: 1px;
  font-size: 1em;
  height: 40%;
  color: grey;
}

.button-container {
  margin: 0 auto;
  padding: 20px 0px 0 1px;
  display: inline-block;
}

.button {
  color: black;
  font-size: 1.8em;
  border: none;
  width: 84px;
  margin: 0 0px 15px 0;
  height: 50px;
  background: #efefef;
  border-radius: 4px;
  box-shadow: 0 3px 0px black, 0 -2px 4px rgba(255,255,255, 0.3) inset;
}

.button:focus {
  box-shadow: none;
  transform: translateY(3px);
  border: none;
  outline: none;
}

.red {
  background: #4c4c4c;
  color: white;
}

.tall {
  margin-right: 2px;
  height:115px;
  float: right;
}

.wide {
  width:169px;
}

.clear-float {
  clear: both;
}

#info {
  margin: 0 auto;
}
              
            
!

JS

              
                document.addEventListener("DOMContentLoaded", function () {
  var digitArray = ["0","1","2","3","4","5","6","7","8","9"];
  var operatorArray = ["+", "-", "*", "/"];
  var entryHTML = document.getElementById("entry");
  var historyHTML = document.getElementById("history");
  
  var firstInput = true;
  var firstEntry = true;
  var lastOperator = "";
  var decimalEntered = false;
  var operatorEntered = true;
  var history = "";
  var currentEntry = "";
  var calculation = 0;
  var buttonList = document.getElementsByClassName("button");
  
  function resetHistory() {
    firstInput = true;
    decimalEntered = false;
    operatorEntered = true;
    lastOperator = "";
    calculation = 0;
    history = "";
    currentEntry = "";
    firstEntry = true;
  }
  
  function validEntry(value) {
    if (digitArray.indexOf(value)>=0)
    {
      if (currentEntry.length === 9)
        return 200;
      if (firstEntry === true && value === "0")
        return false;
      else
        return true;
    }
    else if (operatorArray.indexOf(value) >= 0)
      if (operatorEntered === true)
        return false;
      else
        return true;
    else if (value === "=")
      if (operatorArray.indexOf(currentEntry) >= 0)
        return 100;
      else
        return true;
    else if (value == "CE" || value == "AC")
        return true;
  };
  
  function calculateValue(calculation, currentEntry, operator) {
    switch (operator)
    {
      case (""):
        calculation = currentEntry;
        break;
      case ("+"):
        calculation += currentEntry;
        break;
      case ("-"):
        calculation -= currentEntry;
        break;
      case ("*"):
        calculation *= currentEntry;
        break;
      case ("/"):
        calculation /= currentEntry;
        break;
    }
    return calculation;
  }
  
  function entryType(value) {
    if (digitArray.indexOf(String(value)) >= 0)
      return "digit";
    else if (operatorArray.indexOf(value) >= 0)
      return "operator";
    else if (value === ".")
      return "decimal";
    else if (value === "CE" || value ==="AC")
      return "clear";
    else
      return "enter";
  };
  
  function buttonClicked() {
    var entry = this.value;
    var entryStatus = validEntry(entry);
    if (entryStatus === true)
    {
      switch (entryType(entry))
      {
        case ("digit"):
          if (operatorEntered)
            currentEntry = entry;
          else
            currentEntry += entry;
          if (firstInput === true)
          {
            history = entry;
            firstInput = false;
          }
          else
            history += entry;
          operatorEntered = false;
          break;
          
        case ("operator"):
          if (!operatorEntered) calculation = calculateValue(calculation, Number(currentEntry), lastOperator);
          lastOperator = entry;
          history += entry;
          currentEntry = entry;
          operatorEntered = true;
          decimalEntered = false;
          break;
        case ("decimal"):
          history += entry;
          currentEntry += entry;
          decimalEntered = true;
          operatorEntered = false;
        case ("clear"):
          if (entry === "CE")
          {
            decimalEntered = false;
            operatorEntered = true;
            firstEntry = true;
            history = history.slice(0, history.length - currentEntry.length);
            currentEntry = "";
          }
          else
          {
            resetHistory();
          }
          break;
        case ("enter"):
          console.log(calculation, Number(currentEntry), lastOperator);
          calculation = Math.round(calculateValue(calculation, Number(currentEntry), lastOperator)*100)/100;
          var temp = calculation;
          resetHistory();
          currentEntry = temp;
        }
      }
      else if (entryStatus===100)
      {
        resetHistory();
        currentEntry = "Error";
        history = "Unexpected operator";
      }
      else if (entryStatus ===200)
      {
        resetHistory();
        currentEntry = "Overflow";
        history = "Too many digits entered";
      }
        entryHTML.innerText = currentEntry;
        historyHTML.innerText = history;
    }
  for (var i = 0; i<buttonList.length; i++)
  {
    var element = buttonList[i];
    element.addEventListener("click", buttonClicked);
  }
});
              
            
!
999px

Console