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 class="calculator card">

    <input type="text" class="calculator-screen z-depth-1" value="" disabled />

    <div class="calculator-keys">

<button type="button" class="operator btn btn-info" value="+">+</button>
      <button type="button" class="operator btn btn-info" value="-">-</button>
      <button type="button" class="operator btn btn-info" value="*">&times;</button>
      <button type="button" class="operator btn btn-info" value="/">&divide;</button>

      <button type="button" value="7" class="btn btn-light waves-effect">7</button>
      <button type="button" value="8" class="btn btn-light waves-effect">8</button>
      <button type="button" value="9" class="btn btn-light waves-effect">9</button>


      <button type="button" value="4" class="btn btn-light waves-effect">4</button>
      <button type="button" value="5" class="btn btn-light waves-effect">5</button>
      <button type="button" value="6" class="btn btn-light waves-effect">6</button>


      <button type="button" value="1" class="btn btn-light waves-effect">1</button>
      <button type="button" value="2" class="btn btn-light waves-effect">2</button>
      <button type="button" value="3" class="btn btn-light waves-effect">3</button>


      <button type="button" value="0" class="btn btn-light waves-effect">0</button>
      <button type="button" class="decimal function btn btn-secondary" value=".">.</button>
      <button type="button" class="all-clear function btn btn-danger btn-sm" value="all-clear">DEL</button>

      <button type="button" class="equal-sign operator btn btn-default" value="=">=</button>

    </div>
  </div>
</div>
              
            
!

CSS

              
                 html {
      font-size: 80%;
      box-sizing: border-box;
      background-color: #FFF;
    }

    *,
    *::before,
    *::after {
      margin: 0;
      padding: 0;
      box-sizing: inherit;
    }

    .calculator {
      border: 5px solid #137dc1;
      border-radius: 5px;
      position: absolute;
      top: 55%;
      left: 55%;
      transform: translate(-50%, -50%);
      width: 400px;
    }

    .calculator-screen {
      width: 100%;
      height: 80px;
      border: none;
      background-color: #137dc1;
      color: #fff;
      text-align: right;
      padding-right: 20px;
      padding-left: 10px;
      font-size: 4rem;
    }

    button {
      height: 60px;
      font-size: 2rem!important;
    }

    .equal-sign {
      height: 50%;
      grid-area: 2 / 4 / 6 / 5;
    }

    .calculator-keys {
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      grid-gap: 10px;
      padding: 40px;
    }
              
            
!

JS

              
                const calculator = {
  displayValue: '0',
  firstOperand: null,
  waitingForSecondOperand: false,
  operator: null,
};

function inputDigit(digit) {
  const { displayValue, waitingForSecondOperand } = calculator;

  if (waitingForSecondOperand === true) {
    calculator.displayValue = digit;
    calculator.waitingForSecondOperand = false;
  } else {
    calculator.displayValue = displayValue === '0' ? digit : displayValue + digit;
  }
}

function inputDecimal(dot) {
  // If the `displayValue` does not contain a decimal point
  if (!calculator.displayValue.includes(dot)) {
    // Append the decimal point
    calculator.displayValue += dot;
  }
}

function handleOperator(nextOperator) {
  const { firstOperand, displayValue, operator } = calculator
  const inputValue = parseFloat(displayValue);

  if (operator && calculator.waitingForSecondOperand)  {
    calculator.operator = nextOperator;
    return;
  }

  if (firstOperand == null) {
    calculator.firstOperand = inputValue;
  } else if (operator) {
    const currentValue = firstOperand || 0;
    const result = performCalculation[operator](currentValue, inputValue);

    calculator.displayValue = String(result);
    calculator.firstOperand = result;
  }

  calculator.waitingForSecondOperand = true;
  calculator.operator = nextOperator;
}

const performCalculation = {
  '/': (firstOperand, secondOperand) => firstOperand / secondOperand,

  '*': (firstOperand, secondOperand) => firstOperand * secondOperand,

  '+': (firstOperand, secondOperand) => firstOperand + secondOperand,

  '-': (firstOperand, secondOperand) => firstOperand - secondOperand,

  '=': (firstOperand, secondOperand) => secondOperand
};

function resetCalculator() {
  calculator.displayValue = '0';
  calculator.firstOperand = null;
  calculator.waitingForSecondOperand = false;
  calculator.operator = null;
}

function updateDisplay() {
  const display = document.querySelector('.calculator-screen');
  display.value = calculator.displayValue;
}

updateDisplay();

const keys = document.querySelector('.calculator-keys');
keys.addEventListener('click', (event) => {
  const { target } = event;
  if (!target.matches('button')) {
    return;
  }

  if (target.classList.contains('operator')) {
    handleOperator(target.value);
		updateDisplay();
    return;
  }

  if (target.classList.contains('decimal')) {
    inputDecimal(target.value);
		updateDisplay();
    return;
  }

  if (target.classList.contains('all-clear')) {
    resetCalculator();
		updateDisplay();
    return;
  }

  inputDigit(target.value);
  updateDisplay();
});
              
            
!
999px

Console