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.main
  div.calculator
    //- title
    div.title-container
      h1 Javascript Calculator
    //- display
    div#container.display  
      //- main
      div.display__main
        //- operator
        div#operator.operator
        //- number
        div#display-container.numeric
      //- result
      div#sub-display-container.display__sub
    //- numeric pad
    div.pad
      a#btn-7.pad__button 7 
      a#btn-8.pad__button 8
      a#btn-9.pad__button 9
      a#btn-div.pad__button.accent /
      a#btn-4.pad__button 4
      a#btn-5.pad__button 5
      a#btn-6.pad__button 6
      a#btn-mult.pad__button.accent *
      a#btn-1.pad__button 1
      a#btn-2.pad__button 2
      a#btn-3.pad__button 3
      a#btn-subs.pad__button.accent -
      a#btn-dot.pad__button .
      a#btn-0.pad__button 0
      a#btn-clr.pad__button clear  
      a#btn-add.pad__button.accent +
      a#btn-equal.pad__button =
              
            
!

CSS

              
                // variables
$max-width: 350px;
$padding-calculator: 20px;
$padding-display: 10px;
$color-background: #5da5e3;
$color-calculator: #504D68;
$color-pad: #9A96A4;
$color-accent: #892B75;

// fonts
@import url('https://fonts.googleapis.com/css?family=Press+Start+2P');

body {
  margin: 0;
  padding: 0;  
  background-image: linear-gradient(to right, #e9dfc4 0%, #e9dfc4 1%, #ede3c8 2%, #ede3c8 24%, #ebddc3 25%, #e9dfc4 48%, #ebddc3 49%, #e6d8bd 52%, #e6d8bd 53%, #e9dbc0 54%, #e6d8bd 55%, #e6d8bd 56%, #e9dbc0 57%, #e6d8bd 58%, #e6d8bd 73%, #e9dbc0 74%, #e9dbc0 98%, #ebddc3 100%);
  background-size: 100px;
  background-repeat: repeat;
}

.main {
  width: 100%;
  margin: 10vh 0;  
}

.calculator {
  display: block;  
  margin: 0 auto;
  background-color: $color-calculator;
  padding: $padding-calculator;  
  width: $max-width;  
}

.title-container {
  font-family: arial; 
  max-width: $max-width;
  text-align: center;
  color: #fff;
}

.display {
  max-width: $max-width;
  font-family: 'Press Start 2P', cursive;
  color: #3A503F;
  background: #669966;  
  
  &__main {
    display: flex;
    justify-content: space-between;  
    margin: 1em 0;
    height: 2em;
    line-height: 2;     
    padding: $padding-display; 
  }
  
  &__sub {
    margin: 1em 0;
    height: 3em;
    line-height: 4;    
    text-align: right;
    padding-right: 1em;
  }
}

.pad {
  max-width: $max-width;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-column-gap: 15px;
  grid-row-gap: 15px; 
  
  &__button {
    background: $color-pad; 
    font-family: Arial;
    color: #fff;
    font-size: 20px;
    padding: 10px 20px 10px 20px;
    text-decoration: none;
    text-align: center;    
  }
  
  &__button:hover {
    background-color: darken($color-pad, 10%);
    color: darken(#fff, 10%);
    text-decoration: none;
    cursor: pointer;
  }
  
  &__button:last-child {
    grid-column: 1 / span 4;
  }

  .accent {
    background-color: $color-accent;
    &:hover {
      background-color: darken($color-accent, 10%);
    }
  }
}
              
            
!

JS

              
                "use strict";

(() => {
  // class 
  class Calculator {
    constructor() {
      this.result = 0;
      this.operator = "";
      this.displayVal1 = 0;
      this.displayVal2 = 0;
      this.chaining = false;
      this.secVal = false;
    }

    getResult() {
      return this.result;
    }

    setDisplayVal1(val) {
      this.displayVal1 = parseFloat(val);
      console.log(`dispVal1: ${this.displayVal1}`);
    }

    setDisplayVal2(val) {
      this.displayVal2 = parseFloat(val);
      console.log(`dispVal2: ${this.displayVal2}`);
    }

    setOperator(op) {
      this.operator = op;
      this.secVal = true;
      console.log(`secVal: ${this.secVal}`);
    }
    // resolve and put the value in this.result
    doOperation() {
      let val1 = this.displayVal1, val2 = this.displayVal2;
      console.log(`opval1 ${val1} -- OPval2 ${val2}`);
      switch (this.operator) {
        case "+":
          this.add(val1, val2);
          break;
        case "-":
          this.substract(val1, val2);
          break;
        case "*":
          this.multiply(val1, val2);
          break;
        case "/":
          this.divide(val1, val2);
          break;
        default:
          console.log("issue :(");
          break;
      }
      this.secVal = true;
    }

    add(val1, val2) {
      this.result = val1 + val2;
    }

    substract(val1, val2) {
      this.result = val1 - val2;
    }

    multiply(val1, val2) {
      this.result = val1 * val2;
      // this.result = this.result.toPrecision(3);
    }

    divide(val1, val2) {
      if (val2 === 0) this.result = "infinity";
      else this.result = val1 / val2;
    }

    clearAll() {
      // this.result = 0;
      // this.operator = "";
      // this.displayVal1 = 0;
      // this.displayVal2 = 0;
    }
  }
  // view
  let c = new Calculator();
  let btnAdd = document.getElementById("btn-add"),
    btnSubs = document.getElementById("btn-subs"),
    btnMult = document.getElementById("btn-mult"),
    btnDiv = document.getElementById("btn-div"),
    btnEqual = document.getElementById("btn-equal"),
    btnClr = document.getElementById("btn-clr"),
    btnDot = document.getElementById("btn-dot"),
    display = document.getElementById("display-container"),
    subDisplay = document.getElementById("sub-display-container"),
    opDisplay = document.getElementById("operator");
  let numPad = [], operations = [btnAdd, btnSubs, btnMult, btnDiv];
  for (let i = 0; i < 10; i++) {
    let tmp = document.getElementById(`btn-${i}`);
    numPad.push(tmp);
  }
  for (let pos of numPad) {
    pos.addEventListener("click", () => {
      display.innerText += pos.textContent;
    });
  }
  // do the magic
  for (let op of operations) {
    op.addEventListener("click", () => {
      // chaining 
      if (c.chaining === true && display.textContent !== '') {
        c.setDisplayVal2(display.textContent);
        display.innerText = '';
        c.doOperation();
        c.setDisplayVal1(c.result);
        c.setOperator(op.textContent);
        subDisplay.innerText = c.displayVal1; //error de apretar dos veces op -> NaN
        display.innerText = '';
        opDisplay.innerText = op.textContent;
        //crear nodo dentro del div-display con text operador (+-/*)
      }
      else if (display.textContent !== '') { // do it only one time
        c.setDisplayVal1(display.textContent);
        c.setOperator(op.textContent);
        c.chaining = true; // enable the if block
        display.innerText = '';
        opDisplay.innerText = op.textContent;
      }
    });
  }

  btnDot.addEventListener("click", () => {
    let dot = btnDot.textContent;
    if (display.innerText.indexOf(".") === -1 && display.innerText !== "") {
      display.innerText += dot;
    }
  });

  btnClr.addEventListener("click", () => {
    display.textContent = "";
    subDisplay.textContent = "";
    opDisplay.textContent = "";
    c.chaining = false;
    c.result = 0;
    // c.clearAll();
  });

  btnEqual.addEventListener("click", () => {
    let displayTxt = document.getElementById("display-container").innerText;
    if (displayTxt !== "" && c.secVal === true) {
      c.setDisplayVal2(displayTxt);
      c.doOperation();
      subDisplay.innerText = c.getResult();
      //deshabilitar btn equal
      c.secVal = false;
      // c.chaining = 0;
      // preventEventDefault();
    }
  });
})();

              
            
!
999px

Console