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 id="calculator">
  <div class="logo">CASIO</div>
  <div id="solar-panel"></div>
  <div id="screen">
    <div id="display"></div>
    <div id="screen-alt"></div>
  </div>
  <div id="buttons">
    <div id="left-buttons">
      <div class="button" id="clear">AC</div>
      <div id="number-buttons">
        <div class="button number-button" id="one">1</div>
        <div class="button number-button" id="two">2</div>
        <div class="button number-button" id="three">3</div>
        <div class="button number-button" id="four">4</div>
        <div class="button number-button" id="five">5</div>
        <div class="button number-button" id="six">6</div>
        <div class="button number-button" id="seven">7</div>
        <div class="button number-button" id="eight">8</div>
        <div class="button number-button" id="nine">9</div>
        <div class="button number-button" id="decimal">.</div>
        <div class="button number-button" id="zero">0</div>
      </div>
    </div>
    <div id="operator-buttons">
      <div class="button operator-button" id="add">&plus;</div>
      <div class="button operator-button" id="subtract">&minus;</div>
      <div class="button operator-button" id="multiply">&times;</div>
      <div class="button operator-button" id="divide">&divide;</div>
      <div class="button" id="equals">&equals;</div>
    </div>
  </div>
  <div class="clearfix"></div>
</div>
              
            
!

CSS

              
                .clearfix{
  clear:both;
}

#calculator {
  font-family: courier;
  font-weight: 600;
  background-color: #DCDBD7;
  max-width: 230px;
  margin: 80px auto 0;
  padding: 5px;
  border-radius: 5px;
  box-shadow: 4px 6px 20px 0px rgba(0, 0, 0, 0.5), inset -2px -2px 5px 0px #89847e
}

#screen {
  background-color: #D5E2E3;
  padding: 7px 12px;
  margin: 4px;
  text-align: right;
  border: 8px solid #848389;
  border-right-width: 14px;
  border-left-width: 14px;
  border-radius: 2px;
}
#screen-main {
  font-size: 18px;
}
#screen-alt {
  font-size: 12px;
  color: grey;
}
.button {
  background-color: #8E8E90;
  color: #FFF;
  font-weight: 400;
  float: left;
  margin: 7px;
  width: 40px;
  padding-top: 7px;
  padding-bottom: 7px;
  text-align: center;
  border-radius: 3px;
  box-shadow: 1px 1px 5px 0px rgba(0,0,0,0.5);
}

#number-buttons,
#left-buttons,
#operator-buttons {
  display: flex;
  flex-wrap: wrap;
}

#left-buttons {
  max-width: 170px;
  float: left;
}

#operator-buttons {
  flex-direction: column;
  float: right;
}

.operator-button {
  font-size: 20px;
  padding: 4.8px;
  width: auto;
  background-color: #505050;
}
#equals {
  width: 45px;
  background-color: #505050;
}
#clear {
  width: auto;
  padding: 7px 14px;
  background-color: #BD627C;
}
.logo {
  margin: 7px
}
#solar-panel {
  height: 20px;
  width: 70px;
  background-color: #462C2C;
  margin: 5px 8px 15px auto;
}
.flicker {
  animation: flicker 4s;
}

@keyframes flicker
{
  49% {opacity:1;}
  25% {opacity:0;}
  34% {opacity:0;}
  35% {opacity:.5;}
  38% {opacity:0;}
  45% {opacity:.5;}
  74% {opacity:1;}
  75% {opacity:0;}
  84% {opacity:0;}
  85% {opacity:.5;}
  88% {opacity:0;}
  95% {opacity:.5;}
  100% {opacity:0;}
}
              
            
!

JS

              
                var current, previous;
var operator = undefined; 
var operators = ['add', 'subtract', 'divide', 'multiply'];

var buttons = document.getElementById("buttons");
var screen = document.getElementById("display"); 
var screenAlt = document.getElementById("screen-alt"); 

document.addEventListener("DOMContentLoaded", function() {
  // set calc state
  clearCalc();
  
  // add event listeners
  buttons.addEventListener("click", function(e){
    if (e.target.classList.contains("number-button")) { //if number
      updateCurrent(e.target.innerHTML);
    } else if (e.target.classList.contains("operator-button")) { //if operator
      setOperator(e.target.id);
    } else if (e.target.id === "equals") { //if equals
      equals()
    } else if (e.target.getAttribute("id") === "clear") { //if clear
      clearCalc();
    }
    updateScreenAlt();
  });
  
  // add keyboard listener
  document.addEventListener('keydown', (e) => {
    if (e.key=== "1") {document.getElementById("one").click();}
    if (e.key=== "2") {document.getElementById("two").click();}
    if (e.key=== "3") {document.getElementById("three").click();}
    if (e.key=== "4") {document.getElementById("four").click();}
    if (e.key=== "5") {document.getElementById("five").click();}
    if (e.key=== "6") {document.getElementById("six").click();}
    if (e.key=== "7") {document.getElementById("seven").click();}
    if (e.key=== "8") {document.getElementById("eight").click();}
    if (e.key=== "9") {document.getElementById("nine").click();}
    if (e.key=== "0") {document.getElementById("zero").click();}
    if (e.key=== ".") {document.getElementById("decimal").click();}
    if (e.key=== "+") {document.getElementById("add").click();}
    if (e.key=== "-") {document.getElementById("subtract").click();}
    if (e.key=== "*") {document.getElementById("multiply").click();}
    if (e.key=== "/") {document.getElementById("divide").click();}
    if (e.key=== "=") {document.getElementById("equals").click();}
    if (e.key=== "Enter") {document.getElementById("equals").click();}
    if (e.key=== "Escape") {document.getElementById("clear").click();}
  });
  
  // add solar panel flicker
  var solarPanel = document.getElementById("solar-panel");
  solarPanel.addEventListener('mouseenter', function(e) {
    screen.classList.add("flicker");
    screenAlt.classList.add("flicker");
  });
  solarPanel.addEventListener('mouseleave', function(e) {
    screen.classList.remove("flicker");
    screenAlt.classList.remove("flicker");    
  });
});

clearCalc = function() {
  previous = undefined;
  operator = undefined; 
  setCurrent("0");
  updateScreenAlt();
}

setCurrent = function(val) {
  current = val.toString();
  screen.innerHTML = current;
}

updateCurrent = function(val) {
  val = val.toString();

  // if current is too long, flicker
  if (current.length > 15) {
    errorFlicker();
  // if new val is . and dot isn't in there
  } else if (val === "." && current.indexOf(".") === -1) {
    current += val;
  // else if val is . and there is one, ignore this
  } else if (val === ".") {
    errorFlicker();
  // else it's a number
  } else {
    current += val;
  }
  
  // if current has leading 0, remove it
  if (current[0] === "0") {
    current = current.slice(1);
  }
  
  // now current has been updated, update screen
  screen.innerHTML = current;
}

function setOperator(op) {
  // if invalid operator don't set operator
  if (operators.indexOf(op) === -1) {
    errorFlicker();
  // previous is undefined, push current to prevous and set op
  } else if (previous == undefined) {
    previous = parseFloat(current);
    setCurrent(0);
    operator = op;
  // if previous is 0, that means change operator only
  } else if (current == 0) {
    operator = op;
  // if previous is a number, operate and store in prevous
  } else {
    previous = operate();
    setCurrent(0);
    operator = op;
  }
}

function equals() {
  // if operator is equals and previous is undefined, do nothing
  if (previous === undefined) {
  // else operate and store in current, clear previous and operator
  } else {
    setCurrent( operate() );
    previous = undefined;
    operator = undefined;
  } 
}

function operate() {
  currentNum = parseFloat(current)
  
  if (operator === 'add') {
    return previous + currentNum;
  } else if (operator === 'subtract') {
    return previous - currentNum;
  } else if (operator === 'divide') {
    return previous / currentNum;
  } else if (operator === 'multiply') {
    return previous * currentNum;
  }
}

function updateScreenAlt() {
  var newState = "&nbsp;";
  
  if (previous !== undefined) {
    newState += previous;
  }
  
  if (operator === 'add') {
    newState += " &plus;";
  } else if (operator === 'subtract') {
    newState += " &minus;";
  } else if (operator === 'divide') {
    newState += " &divide;";
  } else if (operator === 'multiply') {
    newState += " &times;";
  }
  
  screenAlt.innerHTML = newState;
}

function errorFlicker() {
  console.log("error");
} 
              
            
!
999px

Console