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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <!DOCTYPE html>
<html lang="en-GB">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>JavaScript Calculator</title>
    <link rel="stylesheet" href="css/styles.css">
  </head>
  <body>
    <div class="container">
      <div class="calculator__container" data-behavior="calculator_container">
        <div class="calculator__screen" data-behavior="inputed_number"></div>
        <div class="button__container" data-behavior="button_container">
          <button class="undo__button" data-behavior="undo_button">Del</button>
          <button class="clear__button" data-behavior="clear_button">Clear</button>
          <button class="operator__btn"  data-behavior="input_button-operator">/</button>
          <button class="number__btn" data-behavior="input_button">7</button>
          <button class="number__btn" data-behavior="input_button">8</button>
          <button class="number__btn" data-behavior="input_button">9</button>
          <button class="operator__btn" data-behavior="input_button-operator">*</button>
          <button class="number__btn" data-behavior="input_button">4</button>
          <button class="number__btn" data-behavior="input_button">5</button>
          <button class="number__btn" data-behavior="input_button">6</button>
          <button class="operator__btn" data-behavior="input_button-operator">-</button>
          <button class="number__btn" data-behavior="input_button">1</button>
          <button class="number__btn" data-behavior="input_button">2</button>
          <button class="number__btn" data-behavior="input_button">3</button>
          <button class="operator__btn" data-behavior="input_button-operator">+</button>
          <button class="input__button--zero number__btn" data-behavior="input_button">0</button>
          <button class="period__button period__btn" data-behavior="input_button">.</button>
          <button class="operator__btn" data-behavior="operator_button-equals">=</button>
        </div>
      </div>
    </div>
  </body>
</html>
              
            
!

CSS

              
                /* Reset */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}

article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
  display: block;
}
body {
  line-height: 1;
}
ol, ul {
  list-style: none;
}
blockquote, q {
  quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
  content: '';
  content: none;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
}

/* Base styles */
@font-face {
  font-family: 'Nunito';
  font-style: normal;
  font-weight: 800;
  src: local('Nunito ExtraBold'), local('Nunito-ExtraBold'),
       url('../fonts/nunito-v9-latin-800.woff2') format('woff2'), 
       url('../fonts/nunito-v9-latin-800.woff') format('woff'), 
       url('../fonts/nunito-v9-latin-800.svg#Nunito') format('svg');
}

body {
  background-color: blanchedalmond;
  box-sizing: border-box;
  padding-top: 25px;
  font-size: 16px;
}

.container {
  max-width: 300px;
  margin: 0 auto;
}

h1 {
  font-family: 'Nunito', sans-serif;
  text-align: center;
  font-size: 1.8em;
  margin-bottom: 50px;
  font-family: Nunito;
}

.calculator__container {
  background-color: #000;
  margin: 0 auto;
  max-width: 300px;
  border: 2px solid #111;
  border-radius: 10px;
  overflow: hidden;
  padding: 15px 15px 30px 15px;
  box-shadow: 5px 5px 18px #888888;
}

/* Buttons */
.button__container{
  display: grid;
  grid-template: repeat(5, 1fr) / repeat(4, 1fr);
  grid-gap: 10px;
}

button {
  display: block;
  border: none;
  color: #f2f2f2;
  outline: none;
  height: 55px;
  background-color: #fff;
  border-radius: 10px;
  font-size: 1em;
  font-weight: 600;
}

button:hover {
  cursor: pointer;
  opacity: 0.9;

}

.clear__button, .undo__button {
  background-color: #afafaf;
  color: rgb(26, 24, 24);
}

.clear__button {
  grid-column: span 2;
}

.input__button--zero {
  grid-column: span 2;
}

.number__btn, .period__btn {
  background-color: #505050;
}

.operator__btn{
  background-color: #d34e36; 
}

/* Calculator screen/display */
.calculator__screen {
  color: #f2f2f2;
  font-family: sans-serif;
  background: none;
  border: none;
  box-shadow: none;
  width: 100%;
  height: 25px;
  border-bottom: 2px solid #111;
  padding: 25px 0 20px 0;
  font-size: 2.5em;
  font-weight: 500;
  text-align: right;
  outline: none;
}
              
            
!

JS

              
                
let num1 = ''
let num2 = ''
let operator = ''
let currentTotal = ''

// Number buttons
const numbers = document.querySelectorAll("[data-behavior='input_button']");
numbers.forEach((button) => {
  button.addEventListener('click', (e) => {
    if (!operator) {
      num1 += e.target.innerHTML
    } else {
      num2 += e.target.innerHTML
    }
    updateDisplay(e.target.innerHTML);
  })
})

// Operator buttons
const operators = document.querySelectorAll("[data-behavior='input_button-operator']");
operators.forEach((button) => {
  button.addEventListener('click', (e) => {
    if (num1 && !num2 && !operator) {
      operator = e.target.innerHTML
      clearDisplay()
    }
  })
})

// Equals button
const equals = document.querySelector("[data-behavior='operator_button-equals']")
equals.addEventListener('click', (e) => {
  const result = operate(operator, num1, num2)
  reset()
  if(result %1 > 0) {
    calculatorScreen.innerHTML = result.toFixed(9)
  } else {
    calculatorScreen.innerHTML = result
    num1 = result
  }
})

// Maths formulas
const add = (num1, num2) => {
  return parseInt(num1) + parseInt(num2)
}

const subtract = (num1, num2) => {
  return num1 - num2
}

const multiply = (num1, num2) => {
  return num1 * num2
}

const divide = (num1, num2) => {
  return num1 / num2
}

// Calculations
const operate = (operator, num1, num2) => {
  switch (operator) {
    case "+": 
      return add(num1, num2)
      break;
    case "-":
      return subtract(num1, num2)
      break;
    case "*": 
      return multiply(num1, num2)
      break; 
    case "/":
      return divide(num1, num2)
      break; 
    default: 
      reset()
  }
}

// Updates screen
const calculatorScreen = document.querySelector("[data-behavior='inputed_number']")
updateDisplay = (input) => {
  calculatorScreen.append(input)
}

// Resets the display 
const clear = document.querySelector("[data-behavior='clear_button']")
clear.addEventListener('click', (e) => {
  reset()
})

const reset = () => {
  num1 = ''
  num2 = ''
  operator = ''
  clearDisplay()
}

const clearDisplay = () => {
  calculatorScreen.innerHTML = ''
}

// Deletes the last number on the screen
const deleteLastNum = document.querySelector("[data-behavior='undo_button']")
deleteLastNum.addEventListener('click', (e) => {
  if (num1 && num2) {
    num1 = num1.slice(0, -1)
  } else if (num1 && num2) {
    num2 = num2.slice(0, -1)
  }
  calculatorScreen.innerHTML = calculatorScreen.innerHTML.slice(0, -1)
})

              
            
!
999px

Console